- Added some options to withJoin() method of repository
This commit is contained in:
parent
9ebf7b05d7
commit
8489cb4841
|
@ -72,8 +72,8 @@ class Column
|
||||||
#[Field(name: "COLUMN_COMMENT", length: 1024)]
|
#[Field(name: "COLUMN_COMMENT", length: 1024)]
|
||||||
public string $comment;
|
public string $comment;
|
||||||
|
|
||||||
#[Field(name: "IS_GENERATED", length: 6)]
|
# #[Field(name: "IS_GENERATED", length: 6)]
|
||||||
public string $generated;
|
# public string $generated;
|
||||||
|
|
||||||
#[Field(name: "GENERATION_EXPRESSION", type: "longtext")]
|
#[Field(name: "GENERATION_EXPRESSION", type: "longtext")]
|
||||||
public ? string $generationExpression;
|
public ? string $generationExpression;
|
||||||
|
|
|
@ -13,6 +13,7 @@ use Ulmus\Annotation\Property\{Field,
|
||||||
WithJoin,
|
WithJoin,
|
||||||
Relation\Ignore as RelationIgnore};
|
Relation\Ignore as RelationIgnore};
|
||||||
use Ulmus\Common\EntityResolver;
|
use Ulmus\Common\EntityResolver;
|
||||||
|
use Ulmus\Repository\WithOptionEnum;
|
||||||
|
|
||||||
class Repository
|
class Repository
|
||||||
{
|
{
|
||||||
|
@ -592,7 +593,7 @@ class Repository
|
||||||
return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value);
|
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) ) {
|
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
|
||||||
$select = $this->entityResolver->fieldList(EntityResolver::KEY_COLUMN_NAME, true);
|
$select = $this->entityResolver->fieldList(EntityResolver::KEY_COLUMN_NAME, true);
|
||||||
|
@ -636,18 +637,24 @@ class Repository
|
||||||
|
|
||||||
$this->open();
|
$this->open();
|
||||||
|
|
||||||
|
if ( ! in_array(WithOptionEnum::SkipWhere, $options)) {
|
||||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Where::class, Where::class ] ) as $condition) {
|
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Where::class, Where::class ] ) as $condition) {
|
||||||
if ( is_object($condition->field) && ( $condition->field->entityClass !== $entity ) ) {
|
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);
|
$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) {
|
if ( ! in_array(WithOptionEnum::SkipHaving, $options)) {
|
||||||
call_user_func_array([ $this->entityClass, $filter->method ], [ $this, $item, true ]);
|
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();
|
$this->close();
|
||||||
|
@ -656,7 +663,8 @@ class Repository
|
||||||
|
|
||||||
$foreignKey = is_string($annotation->foreignKey) ? $entity::field($annotation->foreignKey, $alias) : $annotation->foreignKey;
|
$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) {
|
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Where::class, 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);
|
||||||
|
@ -672,21 +680,24 @@ class Repository
|
||||||
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\FilterJoin::class, FilterJoin::class ]) as $filter) {
|
if ( ! in_array(WithOptionEnum::SkipJoinFilter, $options) ) {
|
||||||
call_user_func_array([ $this->entityClass, $filter->method ], [ $join, $item, true ]);
|
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 {
|
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;
|
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.
|
# We skip subqueries when counting results since it should not affect the row count.
|
||||||
if ( $this instanceof Repository\ServerRequestCountRepository ) {
|
if ( $this instanceof Repository\ServerRequestCountRepository ) {
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Repository;
|
||||||
|
|
||||||
|
enum WithOptionEnum
|
||||||
|
{
|
||||||
|
case SkipWhere;
|
||||||
|
case SkipHaving;
|
||||||
|
case SkipFilter;
|
||||||
|
case SkipOrderBy;
|
||||||
|
case SkipJoinWhere;
|
||||||
|
case SkipJoinFilter;
|
||||||
|
}
|
Loading…
Reference in New Issue