- WIP on migration and some bug fixes linked to withJoin() select problems
This commit is contained in:
1 parent
761e0401b0
commit
cc6048d4ee
20 files changed
+196
-93
No files matched your search
+45
-19
@@ -416,7 +416,8 @@ class Repository implements RepositoryInterface
|
||||
|
||||
public function withJoin(string|array $fields, array $options = []) : self
|
||||
{
|
||||
$canSelect = null === $this->queryBuilder->getFragment(Query\Select::class);
|
||||
$selectObj = $this->queryBuilder->getFragment(Query\Select::class);
|
||||
$canSelect = empty($selectObj) || $selectObj->isInternalSelect === true;
|
||||
|
||||
if ( $canSelect ) {
|
||||
$select = $this->entityResolver->fieldList(EntityResolver::KEY_COLUMN_NAME, true);
|
||||
@@ -424,18 +425,13 @@ class Repository implements RepositoryInterface
|
||||
}
|
||||
|
||||
# @TODO Apply FILTER annotation to this too !
|
||||
foreach(array_filter((array) $fields) as $item) {
|
||||
if ( isset($this->joined[$item]) ) {
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
$this->joined[$item] = true;
|
||||
}
|
||||
foreach(array_filter((array) $fields, fn($e) => ! isset($this->joined[$e])) as $item) {
|
||||
$this->joined[$item] = true;
|
||||
|
||||
$attribute = $this->entityResolver->searchFieldAnnotation($item, [ Join::class ]) ?:
|
||||
$this->entityResolver->searchFieldAnnotation($item, [ Relation::class ]);
|
||||
|
||||
$isRelation = ( $attribute instanceof Relation ) || ($attribute instanceof Relation);
|
||||
$isRelation = $attribute instanceof Relation;
|
||||
|
||||
if ($isRelation && ( $attribute->isManyToMany() )) {
|
||||
throw new \Exception("Many-to-many relation can not be preloaded within joins.");
|
||||
@@ -444,7 +440,7 @@ class Repository implements RepositoryInterface
|
||||
if ( $attribute ) {
|
||||
$alias = $attribute->alias ?? $item;
|
||||
|
||||
$entity = $attribute->entity ?? $this->entityResolver->reflectedClass->getProperties(true)[$item]->getTypes()[0]->type;
|
||||
$entity = $attribute->entity ?? $this->entityResolver->getPropertyEntityType($item);
|
||||
|
||||
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
|
||||
if ( null === $entity::resolveEntity()->searchFieldAnnotation($field->name, [ Relation\Ignore::class ]) ) {
|
||||
@@ -462,12 +458,12 @@ class Repository implements RepositoryInterface
|
||||
$this->open();
|
||||
|
||||
if ( ! in_array(WithOptionEnum::SkipWhere, $options)) {
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ] ) as $condition) {
|
||||
if ( is_object($condition->field) && ( $condition->field->entityClass !== $entity ) ) {
|
||||
$this->where(is_object($condition->field) ? $condition->field : $entity::field($condition->field), $condition->getValue(), $condition->operator);
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ] ) as $condition) {
|
||||
if ( is_object($condition->field) && ( $condition->field->entityClass !== $entity ) ) {
|
||||
$this->where(is_object($condition->field) ? $condition->field : $entity::field($condition->field), $condition->getValue(), $condition->operator);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! in_array(WithOptionEnum::SkipHaving, $options)) {
|
||||
foreach ($this->entityResolver->searchFieldAnnotationList($item, [ Having::class ]) as $condition) {
|
||||
@@ -485,9 +481,9 @@ class Repository implements RepositoryInterface
|
||||
|
||||
$key = is_string($attribute->key) ? $this->entityClass::field($attribute->key) : $attribute->key;
|
||||
|
||||
$foreignKey = is_string($attribute->foreignKey) ? $entity::field($attribute->foreignKey, $alias) : $attribute->foreignKey;
|
||||
$foreignKey = $this->evalClosure($attribute->foreignKey, $entity, $alias);
|
||||
|
||||
$this->join("LEFT", $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias, function($join) use ($item, $entity, $alias, $options) {
|
||||
$this->join($isRelation ? "LEFT" : $attribute->type, $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias, function($join) use ($item, $entity, $alias, $options) {
|
||||
if ( ! in_array(WithOptionEnum::SkipJoinWhere, $options)) {
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ]) as $condition) {
|
||||
if ( ! is_object($condition->field) ) {
|
||||
@@ -518,6 +514,12 @@ class Repository implements RepositoryInterface
|
||||
}
|
||||
}
|
||||
|
||||
if ($canSelect) {
|
||||
if ( $selectObj ??= $this->queryBuilder->getFragment(Query\Select::class) ) {
|
||||
$selectObj->isInternalSelect = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -549,7 +551,7 @@ class Repository implements RepositoryInterface
|
||||
->selectJsonEntity($relationRelation->entity, $alias)->open();
|
||||
}
|
||||
else {
|
||||
$entity = $relation->entity ?? $this->entityResolver->properties[$item]['type'];
|
||||
$entity = $relation->entity ?? $this->entityResolver->getPropertyEntityType($name);
|
||||
$repository = $entity::repository()->selectJsonEntity($entity, $alias)->open();
|
||||
}
|
||||
|
||||
@@ -590,7 +592,7 @@ class Repository implements RepositoryInterface
|
||||
$order = $this->entityResolver->searchFieldAnnotationList($name, [ OrderBy::class ]);
|
||||
$where = $this->entityResolver->searchFieldAnnotationList($name, [ Where::class ]);
|
||||
|
||||
$baseEntity = $relation->entity ?? $relation->bridge ?? $this->entityResolver->properties[$name]['type'];
|
||||
$baseEntity = $relation->entity ?? $relation->bridge ?? $this->entityResolver->getPropertyEntityType($name);
|
||||
$baseEntityResolver = $baseEntity::resolveEntity();
|
||||
|
||||
$property = ($baseEntityResolver->field($relation->foreignKey, 01, false) ?: $baseEntityResolver->field($relation->foreignKey, 02))['name'];
|
||||
@@ -644,6 +646,22 @@ class Repository implements RepositoryInterface
|
||||
}
|
||||
}
|
||||
|
||||
## AWAITING THE Closures in constant expression from PHP 8.5 !
|
||||
protected function evalClosure(string $content, string $entityClass, mixed $alias = self::DEFAULT_ALIAS) : mixed
|
||||
{
|
||||
if (is_string($content)) {
|
||||
if ( str_starts_with($content, 'fn(') ) {
|
||||
$closure = eval("return $content;");
|
||||
|
||||
return $closure($this);
|
||||
}
|
||||
|
||||
return $entityClass::field($content, $alias);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest, bool $count = true) : self
|
||||
{
|
||||
if ($count) {
|
||||
@@ -748,7 +766,15 @@ class Repository implements RepositoryInterface
|
||||
|
||||
public function instanciateEntity(? string $entityClass = null) : object
|
||||
{
|
||||
$entity = ( new \ReflectionClass($entityClass ?? $this->entityClass) )->newInstanceWithoutConstructor();
|
||||
$entityClass ??= $this->entityClass;
|
||||
|
||||
try {
|
||||
$entity = new $entityClass();
|
||||
}
|
||||
catch (\Throwable $ex) {
|
||||
$entity = ( new \ReflectionClass($entityClass) )->newInstanceWithoutConstructor();
|
||||
}
|
||||
|
||||
$entity->initializeEntity();
|
||||
|
||||
return $entity;
|
||||
|
||||
Reference in new issue
Block a user