- WIP on attributes ; on testing phase
This commit is contained in:
1 parent
5295a335b3
commit
e6d0279c1a
44 files changed
+763
-142
No files matched your search
+33
-23
@@ -2,7 +2,16 @@
|
||||
|
||||
namespace Ulmus;
|
||||
|
||||
use Ulmus\Annotation\Property\{Field, Where, Having, Relation, Filter, Join, FilterJoin, WithJoin, Relation\Ignore as RelationIgnore};
|
||||
use Ulmus\Annotation\Property\{Field,
|
||||
OrderBy,
|
||||
Where,
|
||||
Having,
|
||||
Relation,
|
||||
Filter,
|
||||
Join,
|
||||
FilterJoin,
|
||||
WithJoin,
|
||||
Relation\Ignore as RelationIgnore};
|
||||
use Ulmus\Common\EntityResolver;
|
||||
|
||||
class Repository
|
||||
@@ -332,7 +341,7 @@ class Repository
|
||||
$dataset = $this->generateDatasetDiff($entity, $oldValues);
|
||||
|
||||
foreach($dataset as $field => $value) {
|
||||
if ( false === ( $this->entityResolver->searchFieldAnnotation($field, new Field, false)->readonly ?? false ) ) {
|
||||
if ( false === ( $this->entityResolver->searchFieldAnnotation($field, [ Attribute\Property\Field::class, Field::class ], false)->readonly ?? false ) ) {
|
||||
$intersect[$field] = $field;
|
||||
}
|
||||
}
|
||||
@@ -367,7 +376,7 @@ class Repository
|
||||
$prependField and ($prependField .= "$");
|
||||
|
||||
foreach ($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
|
||||
if (null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore)) {
|
||||
if (null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], [ Attribute\Property\Relation\Ignore::class, RelationIgnore::class ])) {
|
||||
$this->select(sprintf("%s.$key as {$prependField}{$field['name']}", $this->escapeIdentifier($alias)));
|
||||
}
|
||||
}
|
||||
@@ -380,7 +389,7 @@ class Repository
|
||||
$fieldlist = [];
|
||||
|
||||
foreach ($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
|
||||
if (null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore)) {
|
||||
if (null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], [ Attribute\Property\Relation\Ignore::class, RelationIgnore::class ])) {
|
||||
$fieldlist[] = $key;
|
||||
$fieldlist[] = $entity::field($field['name'], $this->escapeIdentifier($alias));
|
||||
}
|
||||
@@ -574,7 +583,7 @@ class Repository
|
||||
return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value);
|
||||
}
|
||||
|
||||
public function withJoin(/*string|array*/ $fields) : self
|
||||
public function withJoin(string|array $fields) : self
|
||||
{
|
||||
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
|
||||
$this->select("{$this->alias}.*");
|
||||
@@ -582,10 +591,12 @@ class Repository
|
||||
|
||||
# @TODO Apply FILTER annotation to this too !
|
||||
foreach(array_filter((array) $fields) as $item) {
|
||||
$annotation = $this->entityResolver->searchFieldAnnotation($item, new Join) ?:
|
||||
$this->entityResolver->searchFieldAnnotation($item, new Relation);
|
||||
$annotation = $this->entityResolver->searchFieldAnnotation($item, [ Attribute\Property\Join::class, Join::class ]) ?:
|
||||
$this->entityResolver->searchFieldAnnotation($item, [ Attribute\Property\Relation::class, Relation::class ]);
|
||||
|
||||
if (( $annotation instanceof Relation ) && ( $annotation->normalizeType() === 'manytomany' )) {
|
||||
$isRelation = ( $annotation instanceof Relation ) || ($annotation instanceof Attribute\Property\Relation);
|
||||
|
||||
if ($isRelation && ( $annotation->normalizeType() === 'manytomany' )) {
|
||||
throw new Exception("Many-to-many relation can not be preloaded within joins.");
|
||||
}
|
||||
|
||||
@@ -595,10 +606,10 @@ class Repository
|
||||
$entity = $annotation->entity ?? $this->entityResolver->properties[$item]['type'];
|
||||
|
||||
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
|
||||
if ( null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore) ) {
|
||||
if ( null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], [ Attribute\Property\Relation\Ignore::class, RelationIgnore::class ]) ) {
|
||||
$escAlias = $this->escapeIdentifier($alias);
|
||||
|
||||
$name = $entity::resolveEntity()->searchFieldAnnotation($field['name'], new Field())->name ?? $field['name'];
|
||||
$name = $entity::resolveEntity()->searchFieldAnnotation($field['name'], [ Attribute\Property\Field::class, Field::class ])->name ?? $field['name'];
|
||||
|
||||
$this->select("$escAlias.$key as $alias\${$name}");
|
||||
}
|
||||
@@ -606,17 +617,17 @@ class Repository
|
||||
|
||||
$this->open();
|
||||
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, new Where() ) as $condition) {
|
||||
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->value, $condition->operator);
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, new Having() ) as $condition) {
|
||||
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->value, $condition->operator);
|
||||
}
|
||||
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, new Filter() ) as $filter) {
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Filter::class, Filter::class ] ) as $filter) {
|
||||
call_user_func_array([ $this->entityClass, $filter->method ], [ $this, $item, true ]);
|
||||
}
|
||||
|
||||
@@ -627,8 +638,7 @@ 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) {
|
||||
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, new Where() ) as $condition) {
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Where::class, Where::class ]) as $condition) {
|
||||
if ( ! is_object($condition->field) ) {
|
||||
$field = $this->entityClass::field($condition->field);
|
||||
}
|
||||
@@ -644,7 +654,7 @@ class Repository
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, new FilterJoin() ) as $filter) {
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\FilterJoin::class, FilterJoin::class ]) as $filter) {
|
||||
call_user_func_array([ $this->entityClass, $filter->method ], [ $join, $item, true ]);
|
||||
}
|
||||
});
|
||||
@@ -670,7 +680,7 @@ class Repository
|
||||
|
||||
# Apply FILTER annotation to this too !
|
||||
foreach(array_filter((array) $fields) as $item) {
|
||||
if ( $relation = $this->entityResolver->searchFieldAnnotation($item, new Relation) ) {
|
||||
if ( $relation = $this->entityResolver->searchFieldAnnotation($item, [ Attribute\Property\Relation::class, Relation::class ]) ) {
|
||||
$alias = $relation->alias ?? $item;
|
||||
|
||||
if ( $relation->isManyToMany() ) {
|
||||
@@ -691,11 +701,11 @@ class Repository
|
||||
|
||||
# $relation->isManyToMany() and $repository->selectJsonEntity($relation->bridge, $relation->bridgeField, true);
|
||||
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, new Where() ) as $condition) {
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Where::class, Where::class ]) as $condition) {
|
||||
$repository->where(is_object($condition->field) ? $condition->field : $entity::field($condition->field), $condition->value, $condition->operator);
|
||||
}
|
||||
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, new Having() ) as $condition) {
|
||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Attribute\Property\Having::class, Having::class ] ) as $condition) {
|
||||
$repository->having(is_object($condition->field) ? $condition->field : $entity::field($condition->field), $condition->value, $condition->operator);
|
||||
}
|
||||
|
||||
@@ -722,9 +732,9 @@ class Repository
|
||||
public function loadCollectionRelation(EntityCollection $collection, array|string $fields) : void
|
||||
{
|
||||
foreach ((array)$fields as $name) {
|
||||
if (null !== ($relation = $this->entityResolver->searchFieldAnnotation($name, new Annotation\Property\Relation()))) {
|
||||
$order = $this->entityResolver->searchFieldAnnotationList($name, new Annotation\Property\OrderBy());
|
||||
$where = $this->entityResolver->searchFieldAnnotationList($name, new Annotation\Property\Where());
|
||||
if (null !== ($relation = $this->entityResolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ] ))) {
|
||||
$order = $this->entityResolver->searchFieldAnnotationList($name, [ Attribute\Property\OrderBy::class, OrderBy::class ]);
|
||||
$where = $this->entityResolver->searchFieldAnnotationList($name, [ Attribute\Property\Where::class, Where::class ]);
|
||||
|
||||
$baseEntity = $relation->entity ?? $relation->bridge ?? $this->entityResolver->properties[$name]['type'];
|
||||
$baseEntityResolver = $baseEntity::resolveEntity();
|
||||
@@ -735,7 +745,7 @@ class Repository
|
||||
$repository = $baseEntity::repository();
|
||||
|
||||
foreach ($baseEntityResolver->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
|
||||
if (null === $baseEntityResolver->searchFieldAnnotation($field['name'], new RelationIgnore)) {
|
||||
if (null === $baseEntityResolver->searchFieldAnnotation($field['name'], [ Attribute\Property\Relation\Ignore::class, RelationIgnore::class ])) {
|
||||
$repository->select($baseEntityResolver->entityClass::field($key));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user