- Moved withJoin() method of repository into a trait

This commit is contained in:
Dave Mc Nicoll committed 2026-10-22 17:16:36 +00:00
1 parent caf5bd9664
commit c417b00ed4
10 files changed
+205 -118

No files matched your search

+3 -108
View File
@@ -8,12 +8,13 @@ use Ulmus\Attribute\Property\{
use Psr\SimpleCache\CacheInterface;
use Ulmus\Common\EntityResolver;
use Ulmus\Entity\EntityInterface;
use Ulmus\Repository\JoinBuilder;
use Ulmus\Repository\RepositoryInterface;
use Ulmus\Repository\WithOptionEnum;
class Repository implements RepositoryInterface
{
use EventTrait, CacheTrait, Repository\ConditionTrait, Repository\EscapeTrait, Repository\QueryBuildingTrait;
use EventTrait, CacheTrait, Repository\ConditionTrait, Repository\EscapeTrait, Repository\QueryBuildingTrait, Repository\JoinBuilderTrait;
const DEFAULT_ALIAS = "this";
@@ -263,7 +264,7 @@ class Repository implements RepositoryInterface
$entity->entityFillFromDataset($dataset, true);
$this->eventExecute(Event\Query\Update::class, $this, $entity, $dataset, $replace);
$this->eventExecute(Event\Query\Update::class, $this, $entity, $dataset, $diff, $replace);
return $update ? (bool) $update->rowCount : false;
}
@@ -450,112 +451,6 @@ class Repository implements RepositoryInterface
return $this->where($this->entityClass::field($primaryKeyField[$pkField]->name ?? $pkField, $alias), $value);
}
public function withJoin(string|array $fields, array $options = []) : self
{
$canSelect = null === $this->queryBuilder->getFragment(Query\Select::class);
if ( $canSelect ) {
$select = $this->entityResolver->fieldList(EntityResolver::KEY_COLUMN_NAME, true);
$this->select($this->entityClass::fields(array_map(fn($f) => $f['object']->name ?? $f['name'], $select), select: true));
}
$fields = (array) $fields;
usort($fields, fn($e1, $e2) => str_contains($e1, '.') <=> str_contains($e2, '.'));
# @TODO Apply fff annotation to this too !
foreach(array_filter($fields, fn($e) => $e && ! isset($this->joined[$e]) ) as $item) {
$this->joined[$item] = true;
$attribute = clone ($this->entityResolver->searchFieldAnnotation($item, [ Join::class ]) ?:
$this->entityResolver->searchFieldAnnotation($item, [ Relation::class ]));
$isRelation = $attribute instanceof Relation;
if ($isRelation && ( $attribute->isManyToMany() )) {
throw new \Exception("Many-to-many relation can not be preloaded within joins.");
}
if ( $attribute ) {
$alias = $attribute->alias ?? $item;
$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 ]) ) {
$escAlias = $this->escapeIdentifier($alias);
$fieldName = $this->escapeIdentifier($key);
$name = $entity::resolveEntity()->searchFieldAnnotation($field->name, [ Field::class ])->name ?? $field->name;
if ($canSelect) {
# @TODO add handling of the new #[Select] attribute here !
$this->select("$escAlias.$fieldName as $alias\${$name}");
}
}
}
$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);
}
}
}
if ( ! in_array(WithOptionEnum::SkipHaving, $options)) {
foreach ($this->entityResolver->searchFieldAnnotationList($item, [ Having::class ]) as $condition) {
$this->having(is_object($condition->field) ? $condition->field : $entity::field($condition->field), $condition->getValue(), $condition->operator);
}
}
if ( ! in_array(WithOptionEnum::SkipFilter, $options)) {
foreach ($this->entityResolver->searchFieldAnnotationList($item, [ Filter::class ]) as $filter) {
call_user_func_array([$this->entityClass, $filter->method], [$this, $item, true]);
}
}
$this->close();
$key = is_string($attribute->key) ? $this->entityClass::field($attribute->key) : $attribute->key;
$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) {
if ( ! in_array(WithOptionEnum::SkipJoinWhere, $options)) {
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ]) as $condition) {
if ( ! is_object($condition->field) ) {
$field = $this->entityClass::field($condition->field);
}
else {
$field = clone $condition->field;
}
# Adding directly
if ( $field->entityClass === $entity ) {
$field->alias = $alias;
$join->where(is_object($field) ? $field : $entity::field($field, $alias), $condition->getValue(), $condition->operator);
}
}
}
if ( ! in_array(WithOptionEnum::SkipJoinFilter, $options) ) {
foreach ($this->entityResolver->searchFieldAnnotationList($item, [ FilterJoin::class ]) as $filter) {
call_user_func_array([$this->entityClass, $filter->method], [$join, $item, true]);
}
}
});
}
else {
throw new \Exception("Referenced field `$item` which do not exist or do not contain a valid @Join or @Relation attribute.");
}
}
return $this;
}
public function withSubquery(string|array $fields, array $options = []) : self
{
# We skip subqueries when counting results since it should not affect the row count.