- Added some options to withJoin() method of repository

This commit is contained in:
Dave Mc Nicoll 2023-08-29 16:32:25 -04:00
parent 9ebf7b05d7
commit 8489cb4841
3 changed files with 37 additions and 13 deletions

View File

@ -72,8 +72,8 @@ class Column
#[Field(name: "COLUMN_COMMENT", length: 1024)]
public string $comment;
#[Field(name: "IS_GENERATED", length: 6)]
public string $generated;
# #[Field(name: "IS_GENERATED", length: 6)]
# public string $generated;
#[Field(name: "GENERATION_EXPRESSION", type: "longtext")]
public ? string $generationExpression;

View File

@ -13,6 +13,7 @@ use Ulmus\Annotation\Property\{Field,
WithJoin,
Relation\Ignore as RelationIgnore};
use Ulmus\Common\EntityResolver;
use Ulmus\Repository\WithOptionEnum;
class Repository
{
@ -592,7 +593,7 @@ class Repository
return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value);
}
public function withJoin(string|array $fields) : self
public function withJoin(string|array $fields, array $options = []) : self
{
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
$select = $this->entityResolver->fieldList(EntityResolver::KEY_COLUMN_NAME, true);
@ -636,18 +637,24 @@ class Repository
$this->open();
if ( ! in_array(WithOptionEnum::SkipWhere, $options)) {
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Where::class, 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, [ Attribute\Property\Having::class, Having::class ] ) as $condition) {
$this->having(is_object($condition->field) ? $condition->field : $entity::field($condition->field), $condition->getValue(), $condition->operator);
}
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Filter::class, Filter::class ] ) as $filter) {
call_user_func_array([ $this->entityClass, $filter->method ], [ $this, $item, true ]);
if ( ! in_array(WithOptionEnum::SkipHaving, $options)) {
foreach ($this->entityResolver->searchFieldAnnotationList($item, [Attribute\Property\Having::class, 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, [Attribute\Property\Filter::class, Filter::class]) as $filter) {
call_user_func_array([$this->entityClass, $filter->method], [$this, $item, true]);
}
}
$this->close();
@ -656,7 +663,8 @@ class Repository
$foreignKey = is_string($annotation->foreignKey) ? $entity::field($annotation->foreignKey, $alias) : $annotation->foreignKey;
$this->join("LEFT", $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias, function($join) use ($item, $entity, $alias) {
$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, [ Attribute\Property\Where::class, Where::class ]) as $condition) {
if ( ! is_object($condition->field) ) {
$field = $this->entityClass::field($condition->field);
@ -672,21 +680,24 @@ class Repository
$join->where(is_object($field) ? $field : $entity::field($field, $alias), $condition->getValue(), $condition->operator);
}
}
}
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\FilterJoin::class, FilterJoin::class ]) as $filter) {
call_user_func_array([ $this->entityClass, $filter->method ], [ $join, $item, true ]);
if ( ! in_array(WithOptionEnum::SkipJoinFilter, $options) ) {
foreach ($this->entityResolver->searchFieldAnnotationList($item, [Attribute\Property\FilterJoin::class, 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 annotation.");
throw new \Exception("Referenced field `$item` which do not exist or do not contain a valid @Join or @Relation annotation.");
}
}
return $this;
}
public function withSubquery(string|array $fields) : self
public function withSubquery(string|array $fields, array $options = []) : self
{
# We skip subqueries when counting results since it should not affect the row count.
if ( $this instanceof Repository\ServerRequestCountRepository ) {

View File

@ -0,0 +1,13 @@
<?php
namespace Ulmus\Repository;
enum WithOptionEnum
{
case SkipWhere;
case SkipHaving;
case SkipFilter;
case SkipOrderBy;
case SkipJoinWhere;
case SkipJoinFilter;
}