Skip to content

Commit

Permalink
Merge f6ed106 into 7353d17
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jul 7, 2020
2 parents 7353d17 + f6ed106 commit 6550373
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Repository/IRepository.php
Expand Up @@ -156,7 +156,7 @@ public function findBy(array $conds): ICollection;
* @param mixed[] $ids
* @phpstan-param list<mixed> $ids
*/
public function findById($ids): ICollection;
public function findByIds(array $ids): ICollection;


/**
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/IdentityMap.php
Expand Up @@ -64,7 +64,7 @@ public function hasById($id): bool
public function getById($id)
{
$idHash = $this->getIdHash($id);
if (!isset($this->entities[$idHash])) {
if (!isset($this->entities[$idHash]) || isset($this->entitiesForRefresh[$idHash])) {
return null;
}

Expand Down
44 changes: 37 additions & 7 deletions src/Repository/Repository.php
Expand Up @@ -10,6 +10,7 @@
namespace Nextras\Orm\Repository;


use Nextras\Orm\Collection\ArrayCollection;
use Nextras\Orm\Collection\Functions\AvgAggregateFunction;
use Nextras\Orm\Collection\Functions\CompareEqualsFunction;
use Nextras\Orm\Collection\Functions\CompareGreaterThanEqualsFunction;
Expand Down Expand Up @@ -39,6 +40,7 @@
use Nextras\Orm\NoResultException;
use Nextras\Orm\NotImplementedException;
use ReflectionClass;
use function count;


abstract class Repository implements IRepository
Expand Down Expand Up @@ -193,7 +195,7 @@ public function getMapper(): IMapper
/** {@inheritdoc} */
public function getBy(array $conds): ?IEntity
{
return call_user_func_array([$this->findAll(), 'getBy'], func_get_args());
return $this->findAll()->getBy($conds);
}


Expand All @@ -213,8 +215,6 @@ public function getById($id): ?IEntity
{
if ($id === null) {
return null;
} elseif ($id instanceof IEntity) {
$id = $id->getValue('id');
}

$entity = $this->identityMap->getById($id);
Expand Down Expand Up @@ -254,14 +254,44 @@ public function findAll(): ICollection
/** {@inheritdoc} */
public function findBy(array $conds): ICollection
{
return call_user_func_array([$this->findAll(), 'findBy'], func_get_args());
return $this->findAll()->findBy($conds);
}


/** {@inheritdoc} */
/**
* @param array<mixed>|mixed $ids
* @deprecated Use {@see findByIds()}.
*/
public function findById($ids): ICollection
{
return call_user_func_array([$this->findAll(), 'findBy'], [['id' => $ids]]);
if (!is_array($ids)) {
return $this->findByIds([$ids]);
} else {
return $this->findByIds($ids);
}
}


/** {@inheritdoc} */
public function findByIds(array $ids): ICollection
{
$entities = [];
$missingEntities = false;

foreach ($ids as $id) {
$entity = $this->identityMap->getById($id);
if ($entity === null || $entity === false) {
$missingEntities = true;
break;
}
$entities[] = $entity;
}

if (!$missingEntities) {
return new ArrayCollection($entities, $this);
}

return $this->findAll()->findBy(['id' => $ids]);
}


Expand Down Expand Up @@ -504,7 +534,7 @@ public function doRefreshAll(bool $allowOverwrite): void
$ids[] = $entity->getPersistedId();
}
if (count($ids)) {
$this->findById($ids)->fetchAll();
$this->findByIds($ids)->fetchAll();
}
foreach ($entities as $entity) {
if (!$this->identityMap->isMarkedForRefresh($entity)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/integration/Collection/collection.phpt
Expand Up @@ -242,7 +242,7 @@ class CollectionTest extends DataTestCase

public function testCompositePK(): void
{
$followers = $this->orm->tagFollowers->findById([2, 2]);
$followers = $this->orm->tagFollowers->findByIds([[2, 2]]);

Assert::same(1, $followers->count());

Expand All @@ -251,7 +251,7 @@ class CollectionTest extends DataTestCase
Assert::same(2, $follower->tag->id);
Assert::same(2, $follower->author->id);

$followers = $this->orm->tagFollowers->findById([[2, 2], [1, 3]])->orderBy('author');
$followers = $this->orm->tagFollowers->findByIds([[2, 2], [1, 3]])->orderBy('author');

Assert::same(2, $followers->count());

Expand Down

0 comments on commit 6550373

Please sign in to comment.