- 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

+153
View File
@@ -0,0 +1,153 @@
<?php
namespace Ulmus\Repository;
use Ulmus\Attribute\Property\Field;
use Ulmus\Attribute\Property\Filter;
use Ulmus\Attribute\Property\FilterJoin;
use Ulmus\Attribute\Property\Having;
use Ulmus\Attribute\Property\Join;
use Ulmus\Attribute\Property\Relation;
use Ulmus\Attribute\Property\Where;
use Ulmus\Common\EntityField;
use Ulmus\Common\EntityResolver;
use Ulmus\QueryBuilder\QueryBuilderInterface;
use Ulmus\Query;
trait JoinBuilderTrait
{
public function withJoin(string|array $fields, array $options = []) : self
{
$defaultAlias = $this->escapeIdentifier(static::DEFAULT_ALIAS);
$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(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) {
$field = $this->joinFieldObject($condition->field, $this->entityClass);
if ( $field->entityClass === $this->entityClass ) {
$this->where($field, $this->joinFieldValue($condition->getValue(), $alias), $condition->operator);
}
}
}
if ( ! in_array(WithOptionEnum::SkipHaving, $options)) {
foreach ($this->entityResolver->searchFieldAnnotationList($item, [ Having::class ]) as $condition) {
$field = $this->joinFieldObject($condition->field, $this->entityClass);
$this->having($field, $this->joinFieldValue($condition->getValue(), $alias), $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(Query\Join::TYPE_LEFT, $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias, function($join) use ($item, $entity, $alias, $defaultAlias, $options) {
if ( ! in_array(WithOptionEnum::SkipJoinWhere, $options)) {
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ]) as $condition) {
$field = $this->joinFieldObject($condition->field, $this->entityClass);
# Adding directly
if ( $field->entityClass === $entity ) {
if ($field->alias === $defaultAlias) {
$field->alias = $this->escapeIdentifier($alias);
}
$join->where($field, $this->joinFieldValue($condition->getValue(), $alias), $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;
}
protected function joinFieldObject(mixed $field, string $entityClass, string $alias = null) : \Stringable
{
if ( ! is_object($field) ) {
return $entityClass::field($field, $alias);
}
else {
return clone $field;
}
}
protected function joinFieldValue(mixed $value, string $alias = null) : mixed
{
if ( $value instanceof EntityField ) {
$value = clone $value;
$value->alias = $value->alias ?: $this->escapeIdentifier($alias);
return $value;
}
return $value;
}
}