- WIP on notes 2.x
This commit is contained in:
1 parent
2de3139c80
commit
f3be11a590
42 files changed
+270
-820
No files matched your search
@@ -2,20 +2,13 @@
|
||||
|
||||
namespace Ulmus\Repository;
|
||||
|
||||
use Ulmus\{Attribute\Property\Field,
|
||||
Ulmus,
|
||||
Annotation,
|
||||
Attribute,
|
||||
Query,
|
||||
Common,
|
||||
Common\EntityResolver,
|
||||
Repository,
|
||||
Event,
|
||||
EntityCollection};
|
||||
use Ulmus\{
|
||||
Ulmus, Query, Common\EntityResolver, Repository, Event,
|
||||
};
|
||||
|
||||
use Ulmus\Annotation\Property\{Filter, OrderBy, Relation, Relation\Ignore as RelationIgnore, Where, WithJoin, };
|
||||
|
||||
use Closure;
|
||||
use Ulmus\Attribute\Property\{
|
||||
Filter, Join, OrderBy, Relation, Virtual, Where, WithJoin,
|
||||
};
|
||||
|
||||
class RelationBuilder
|
||||
{
|
||||
@@ -64,7 +57,7 @@ class RelationBuilder
|
||||
|
||||
}
|
||||
else {
|
||||
if ( $relation = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class , Relation::class ] ) ) {
|
||||
if ( $relation = $this->resolver->searchFieldAnnotation($name, [ Relation::class ] ) ) {
|
||||
return $this->instanciateEmptyObject($name, $relation);
|
||||
}
|
||||
elseif ($virtual = $this->resolveVirtual($name)) {
|
||||
@@ -77,7 +70,7 @@ class RelationBuilder
|
||||
|
||||
protected function resolveVirtual(string $name) : mixed
|
||||
{
|
||||
if (null !== ($virtual = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Virtual::class, Annotation\Property\Virtual::class ]))) {
|
||||
if (null !== ($virtual = $this->resolver->searchFieldAnnotation($name, Virtual::class))) {
|
||||
try {
|
||||
$arguments = [ $this->entity ];
|
||||
|
||||
@@ -98,11 +91,11 @@ class RelationBuilder
|
||||
|
||||
protected function resolveRelation(string $name) : mixed
|
||||
{
|
||||
if ( null !== ( $relation = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ] ) ) ) {
|
||||
$this->orders = $this->resolver->searchFieldAnnotationList($name, [ Attribute\Property\OrderBy::class, OrderBy::class ] );
|
||||
$this->wheres = $this->resolver->searchFieldAnnotationList($name, [ Attribute\Property\Where::class, Where::class ] );
|
||||
$this->filters = $this->resolver->searchFieldAnnotationList($name, [ Attribute\Property\Filter::class, Filter::class ] );
|
||||
$this->joins = $this->resolver->searchFieldAnnotationList($name, [ Attribute\Property\WithJoin::class, WithJoin::class ] );
|
||||
if ( null !== ( $relation = $this->resolver->searchFieldAnnotation($name, [ Relation::class ] ) ) ) {
|
||||
$this->orders = $this->resolver->searchFieldAnnotationList($name, [ OrderBy::class ] );
|
||||
$this->wheres = $this->resolver->searchFieldAnnotationList($name, [ Where::class ] );
|
||||
$this->filters = $this->resolver->searchFieldAnnotationList($name, [ Filter::class ] );
|
||||
$this->joins = $this->resolver->searchFieldAnnotationList($name, [ WithJoin::class ] );
|
||||
|
||||
switch( true ) {
|
||||
case $relation->isOneToOne():
|
||||
@@ -178,22 +171,22 @@ class RelationBuilder
|
||||
}
|
||||
}
|
||||
|
||||
protected function instanciateEmptyEntity(string $name, Relation|Attribute\Property\Relation $relation) : object
|
||||
protected function instanciateEmptyEntity(string $name, Relation $relation) : object
|
||||
{
|
||||
$class = $relation->entity ?? $this->resolver->properties[$name]['type'];
|
||||
$class = $relation->entity ?? $this->resolver->reflectedClass->getProperties()[$name]->getTypes()[0]->type;
|
||||
|
||||
return new $class();
|
||||
}
|
||||
|
||||
|
||||
protected function instanciateEmptyObject(string $name, Relation|Attribute\Property\Relation $relation) : object
|
||||
protected function instanciateEmptyObject(string $name, Relation $relation) : object
|
||||
{
|
||||
switch( true ) {
|
||||
case $relation->isOneToOne():
|
||||
return $this->instanciateEmptyEntity($name, $relation);
|
||||
|
||||
case $relation->isOneToMany():
|
||||
return ($relation->entity ?? $this->resolver->properties[$name]['type'])::entityCollection();
|
||||
return ($relation->entity ?? $this->resolver->reflectedClass->getProperties()[$name]->getTypes()[0]->type)::entityCollection();
|
||||
|
||||
case $relation->isManyToMany():
|
||||
extract($this->relationAnnotations($name, $relation));
|
||||
@@ -206,14 +199,14 @@ class RelationBuilder
|
||||
|
||||
protected function fetchFromDataset($name, ? array $data = null) : object|bool
|
||||
{
|
||||
$annotation = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Join::class, Annotation\Property\Join::class ]) ?:
|
||||
$this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Annotation\Property\Relation::class ]);
|
||||
$annotation = $this->resolver->searchFieldAnnotation($name, [ Join::class ]) ?:
|
||||
$this->resolver->searchFieldAnnotation($name, [ Relation::class ]);
|
||||
|
||||
if ( $annotation ) {
|
||||
$vars = [];
|
||||
$len = strlen( $name ) + 1;
|
||||
|
||||
$isRelation = ( $annotation instanceof Relation ) || ( $annotation instanceof Attribute\Property\Relation );
|
||||
$isRelation = $annotation instanceof Relation;
|
||||
|
||||
if ( $isRelation && $annotation->isManyToMany() ) {
|
||||
$entity = $this->relationAnnotations($name, $annotation)['relationRelation']->entity;
|
||||
@@ -259,12 +252,12 @@ class RelationBuilder
|
||||
return false;
|
||||
}
|
||||
|
||||
public function oneToOne(string $name, Relation|Attribute\Property\Relation $relation) : Repository
|
||||
public function oneToOne(string $name, Relation $relation) : Repository
|
||||
{
|
||||
return $this->oneToMany($name, $relation)->limit(1);
|
||||
}
|
||||
|
||||
public function oneToMany(string $name, Relation|Attribute\Property\Relation $relation) : Repository
|
||||
public function oneToMany(string $name, Relation $relation) : Repository
|
||||
{
|
||||
$baseEntity = $relation->entity ?? $this->resolver->reflectedClass->getProperties()[$name]->getTypes()[0]->type;
|
||||
|
||||
@@ -290,7 +283,7 @@ class RelationBuilder
|
||||
return $this->applyFilter($this->repository, $name);
|
||||
}
|
||||
|
||||
public function manyToMany(string $name, Relation|Attribute\Property\Relation $relation, Relation|Attribute\Property\Relation & $relationRelation = null, bool $selectBridgeField = true) : Repository
|
||||
public function manyToMany(string $name, Relation $relation, Relation & $relationRelation = null, bool $selectBridgeField = true) : Repository
|
||||
{
|
||||
extract($this->relationAnnotations($name, $relation));
|
||||
|
||||
@@ -318,7 +311,7 @@ class RelationBuilder
|
||||
return $this->applyFilter($this->repository, $name);
|
||||
}
|
||||
|
||||
public static function relationAnnotations(string $name, Relation|Attribute\Property\Relation $relation) : array
|
||||
public static function relationAnnotations(string $name, Relation $relation) : array
|
||||
{
|
||||
if ( $relation->isOneToOne() || $relation->isManyToMany() ) {
|
||||
if ( ! $relation->hasBridge() ) {
|
||||
@@ -326,14 +319,14 @@ class RelationBuilder
|
||||
}
|
||||
|
||||
$bridgeEntity = Ulmus::resolveEntity($relation->bridge);
|
||||
$bridgeRelation = $bridgeEntity->searchFieldAnnotation($relation->field, [ Attribute\Property\Relation::class, Relation::class ]);
|
||||
$relationRelation = $bridgeEntity->searchFieldAnnotation($relation->foreignField, [ Attribute\Property\Relation::class, Relation::class ]);
|
||||
$bridgeRelation = $bridgeEntity->searchFieldAnnotation($relation->field, [ Relation::class ]);
|
||||
$relationRelation = $bridgeEntity->searchFieldAnnotation($relation->foreignField, [ Relation::class ]);
|
||||
|
||||
if ($relationRelation === null) {
|
||||
throw new \Exception("@Relation annotation not found for field `{$relation->foreignField}` in entity {$relation->bridge}");
|
||||
}
|
||||
|
||||
$relationRelation->entity ??= $relation->bridge::resolveEntity()->properties[$relation->foreignField]['type'];
|
||||
$relationRelation->entity ??= $relation->bridge::resolveEntity()->reflectedClass->getProperties()[$relation->foreignField]->getTypes()[0]->type;
|
||||
|
||||
return [
|
||||
'bridgeEntity' => $bridgeEntity,
|
||||
|
||||
Reference in new issue
Block a user