- Added limitation on fixedValue in Where attribute
This commit is contained in:
parent
1fac12c928
commit
589524ca3a
|
@ -13,7 +13,7 @@ class OrWhere extends Where {
|
||||||
public string $operator = Query\Where::OPERATOR_EQUAL,
|
public string $operator = Query\Where::OPERATOR_EQUAL,
|
||||||
public string $condition = Query\Where::CONDITION_OR,
|
public string $condition = Query\Where::CONDITION_OR,
|
||||||
public string|\Stringable|array|null $fieldValue = null,
|
public string|\Stringable|array|null $fieldValue = null,
|
||||||
public null|array $generateValue = null,
|
public null|array|\Closure $generateValue = null,
|
||||||
) {
|
) {
|
||||||
parent::__construct($field, $value, $operator, $condition, $fieldValue, $generateValue);
|
parent::__construct($field, $value, $operator, $condition, $fieldValue, $generateValue);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ class Where {
|
||||||
public string $operator = Query\Where::OPERATOR_EQUAL,
|
public string $operator = Query\Where::OPERATOR_EQUAL,
|
||||||
public string $condition = Query\Where::CONDITION_AND,
|
public string $condition = Query\Where::CONDITION_AND,
|
||||||
public string|\Stringable|array|null $fieldValue = null,
|
public string|\Stringable|array|null $fieldValue = null,
|
||||||
public null|array $generateValue = null,
|
public null|array|\Closure $generateValue = null,
|
||||||
) {
|
) {
|
||||||
$this->field = Attribute::handleArrayField($field);
|
$this->field = Attribute::handleArrayField($field);
|
||||||
$this->fieldValue = Attribute::handleArrayField($fieldValue);
|
$this->fieldValue = Attribute::handleArrayField($fieldValue);
|
||||||
|
@ -29,6 +29,9 @@ class Where {
|
||||||
throw new \Exception(sprintf("Could not generate value from non-instanciated entity for field %s.", (string) $this->field));
|
throw new \Exception(sprintf("Could not generate value from non-instanciated entity for field %s.", (string) $this->field));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
elseif ($this->fieldValue && $entity) {
|
||||||
|
throw new \Exception(sprintf("Field value, from %s, could not be included in query since the entity is already loaded; it is meant to be used with a OneToOne relation loaded within a join.", (string) $this->fieldValue));
|
||||||
|
}
|
||||||
|
|
||||||
return $this->fieldValue ?? $this->value;
|
return $this->fieldValue ?? $this->value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ namespace Ulmus\Entity\InformationSchema;
|
||||||
use Ulmus\EntityCollection,
|
use Ulmus\EntityCollection,
|
||||||
Ulmus\Entity\Field\Datetime;
|
Ulmus\Entity\Field\Datetime;
|
||||||
|
|
||||||
use Ulmus\{Attribute\Obj\Table as TableObj};
|
use Ulmus\{Attribute\Obj\Table as TableObj, Entity\EntityInterface};
|
||||||
use Ulmus\Attribute\Property\{Field, Filter, FilterJoin, Relation, Join, Virtual, Where};
|
use Ulmus\Attribute\Property\{Field, Filter, FilterJoin, Relation, Join, Virtual, Where};
|
||||||
|
|
||||||
#[TableObj(name: "tables", database: "information_schema")]
|
#[TableObj(name: "tables", database: "information_schema")]
|
||||||
|
@ -83,6 +83,12 @@ class Table
|
||||||
public ? string $temporary;
|
public ? string $temporary;
|
||||||
|
|
||||||
#[Relation(type: "oneToMany", key: "name", foreignKey: [ Column::class, 'tableName' ], entity: Column::class)]
|
#[Relation(type: "oneToMany", key: "name", foreignKey: [ Column::class, 'tableName' ], entity: Column::class)]
|
||||||
#[Where('TABLE_SCHEMA', fieldValue: [ Column::class, 'tableSchema' ])]
|
#[Where(field: 'TABLE_SCHEMA', generateValue: [ Table::class, 'getSchema' ])]
|
||||||
public EntityCollection $columns;
|
public EntityCollection $columns;
|
||||||
|
|
||||||
|
# Awaiting PHP 8.5 https://wiki.php.net/rfc/closures_in_const_expr
|
||||||
|
public static function getSchema(Table $entity) : string
|
||||||
|
{
|
||||||
|
return $entity->schema;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -12,6 +12,6 @@ use Ulmus\Attribute\Property\{Field, Filter, FilterJoin, Relation, Join, Virtual
|
||||||
class Table extends \Ulmus\Entity\InformationSchema\Table
|
class Table extends \Ulmus\Entity\InformationSchema\Table
|
||||||
{
|
{
|
||||||
#[Relation(type: "oneToMany", key: "name", foreignKey: [ Column::class, 'tableName' ], entity: Column::class)]
|
#[Relation(type: "oneToMany", key: "name", foreignKey: [ Column::class, 'tableName' ], entity: Column::class)]
|
||||||
#[Where('TABLE_SCHEMA', fieldValue: [ Column::class, 'tableSchema' ])]
|
#[Where('TABLE_SCHEMA', generateValue: [ Table::class, 'getSchema' ])]
|
||||||
public EntityCollection $columns;
|
public EntityCollection $columns;
|
||||||
}
|
}
|
|
@ -476,23 +476,23 @@ class Repository implements RepositoryInterface
|
||||||
$foreignKey = is_string($attribute->foreignKey) ? $entity::field($attribute->foreignKey, $alias) : $attribute->foreignKey;
|
$foreignKey = is_string($attribute->foreignKey) ? $entity::field($attribute->foreignKey, $alias) : $attribute->foreignKey;
|
||||||
|
|
||||||
$this->join("LEFT", $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias, function($join) use ($item, $entity, $alias, $options) {
|
$this->join("LEFT", $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias, function($join) use ($item, $entity, $alias, $options) {
|
||||||
if ( ! in_array(WithOptionEnum::SkipJoinWhere, $options)) {
|
if ( ! in_array(WithOptionEnum::SkipJoinWhere, $options)) {
|
||||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ]) as $condition) {
|
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ]) as $condition) {
|
||||||
if ( ! is_object($condition->field) ) {
|
if ( ! is_object($condition->field) ) {
|
||||||
$field = $this->entityClass::field($condition->field);
|
$field = $this->entityClass::field($condition->field);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$field = clone $condition->field;
|
$field = clone $condition->field;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Adding directly
|
# Adding directly
|
||||||
if ( $field->entityClass === $entity ) {
|
if ( $field->entityClass === $entity ) {
|
||||||
$field->alias = $alias;
|
$field->alias = $alias;
|
||||||
|
|
||||||
$join->where(is_object($field) ? $field : $entity::field($field, $alias), $condition->getValue(), $condition->operator);
|
$join->where(is_object($field) ? $field : $entity::field($field, $alias), $condition->getValue(), $condition->operator);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! in_array(WithOptionEnum::SkipJoinFilter, $options) ) {
|
if ( ! in_array(WithOptionEnum::SkipJoinFilter, $options) ) {
|
||||||
foreach ($this->entityResolver->searchFieldAnnotationList($item, [ FilterJoin::class ]) as $filter) {
|
foreach ($this->entityResolver->searchFieldAnnotationList($item, [ FilterJoin::class ]) as $filter) {
|
||||||
|
@ -594,7 +594,7 @@ class Repository implements RepositoryInterface
|
||||||
|
|
||||||
foreach ($where as $condition) {
|
foreach ($where as $condition) {
|
||||||
# $repository->where($condition->field, is_callable($condition->value) ? call_user_func_array($condition->value, [$this]) : $condition->getValue(), $condition->operator, $condition->condition);
|
# $repository->where($condition->field, is_callable($condition->value) ? call_user_func_array($condition->value, [$this]) : $condition->getValue(), $condition->operator, $condition->condition);
|
||||||
$repository->where($condition->field, $condition->getValue($this), $condition->operator, $condition->condition);
|
$repository->where($condition->field, $condition->getValue(/* why repository sent here ??? $this */), $condition->operator, $condition->condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($order as $item) {
|
foreach ($order as $item) {
|
||||||
|
|
Loading…
Reference in New Issue