From 8e521025d34c56ba6117822f2f5b2d7144db3ef8 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 11 Jun 2020 08:47:15 -0400 Subject: [PATCH] - Some bugfixes made in Relation - Fixed search() methods in EntityCollection - Work done on Relations of EntityTrait ; a lot of work still to be done. It must also move to another file. --- src/Annotation/Property/Relation.php | 8 +++--- src/EntityCollection.php | 39 +++++++++++++++++++++++----- src/EntityTrait.php | 4 +-- src/Repository.php | 4 +-- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/Annotation/Property/Relation.php b/src/Annotation/Property/Relation.php index 5abf696..7456cf9 100644 --- a/src/Annotation/Property/Relation.php +++ b/src/Annotation/Property/Relation.php @@ -6,17 +6,17 @@ class Relation implements \Ulmus\Annotation\Annotation { public string $type; - public string $key; + public /*stringable*/ $key; - public string $foreignKey; + public /*stringable*/ $foreignKey; public array $foreignKeys; public string $bridge; - public string $bridgeKey; + public /*stringable*/ $bridgeKey; - public string $bridgeForeignKey; + public /*stringable*/ $bridgeForeignKey; public string $entity; diff --git a/src/EntityCollection.php b/src/EntityCollection.php index 24d0f7c..91ea2e5 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -14,7 +14,7 @@ class EntityCollection extends \ArrayObject { if ( $callback($item, $key, $idx) ) { $idx++; - yield $item; + yield $key => $item; } } } @@ -30,20 +30,45 @@ class EntityCollection extends \ArrayObject { return $collection; } - public function searchOne($value, string $field) : ? object + public function removeOne($value, string $field, bool $strict = true) : ? object { - # Returning first value only - foreach($this->search($value, $field) as $item) { + foreach($this->search($value, $field, $strict) as $key => $item) { + + $this->offsetUnset($key); + return $item; } return null; } - public function search($value, string $field) : Generator + public function remove($value, string $field, bool $strict = true) : array { - foreach($this->filters(fn($v) => $v->$field === $value) as $item) { - yield $item; + $removed = []; + + foreach($this->search($value, $field, $strict) as $key => $item) { + $this->offsetUnset($key); + + $removed[] = $item; + } + + return $removed; + } + + public function searchOne($value, string $field, bool $strict = true) : ? object + { + # Returning first value only + foreach($this->search($value, $field, $strict) as $item) { + return $item; + } + + return null; + } + + public function search($value, string $field, bool $strict = true) : Generator + { + foreach($this->filters(fn($v) => $strict ? $v->$field === $value : $v->$field == $value) as $key => $item) { + yield $key => $item; } } diff --git a/src/EntityTrait.php b/src/EntityTrait.php index f367a39..1abc13b 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -83,7 +83,7 @@ trait EntityTrait { switch( $relationType ) { case 'onetoone': - $repository->where( $baseEntity->field($relation->foreignKey), $this->$field ); + $repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity->field($relation->foreignKey), $this->$field ); $this->eventExecute(Event\EntityRelationLoadInterface::class, $name, $repository); @@ -96,7 +96,7 @@ trait EntityTrait { return $this->$name = $result[0]; case 'onetomany': - $repository->where( $baseEntity->field($relation->foreignKey), $this->$field); + $repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity->field($relation->foreignKey), $this->$field ); $this->eventExecute(Event\EntityRelationLoadInterface::class, $name, $repository); return $this->$name = call_user_func([$repository, $relation->function]); diff --git a/src/Repository.php b/src/Repository.php index ea89b4f..a19f17c 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -68,12 +68,12 @@ class Repository return Ulmus::runSelectQuery($this->queryBuilder, $this->adapter)->fetchColumn(0); } - public function deleteOne() + protected function deleteOne() { return $this->limit(1)->deleteSqlQuery()->runQuery(); } - public function deleteAll() + protected function deleteAll() { return $this->deleteSqlQuery()->runQuery(); }