- 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.
This commit is contained in:
Dave Mc Nicoll 2020-06-11 08:47:15 -04:00
parent 4d221859fc
commit 8e521025d3
4 changed files with 40 additions and 15 deletions

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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]);

View File

@ -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();
}