Skip to content

Commit

Permalink
fix: create an array containing the entity
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed Jul 2, 2021
1 parent d638902 commit e799aaa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
22 changes: 12 additions & 10 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -6586,7 +6586,7 @@ public function withoutFilter( string $filterClass ): $this
* [delete](#ormentitymanagerdelete) Delete $entity from database
* [describe](#ormentitymanagerdescribe) Returns an array of columns from $table.
* [detach](#ormentitymanagerdetach) Detach $observer from all classes
* [eagerLoad](#ormentitymanagereagerload)
* [eagerLoad](#ormentitymanagereagerload) Load the $relation on all $entities with the least amount of queries
* [escapeIdentifier](#ormentitymanagerescapeidentifier) Returns $identifier quoted for use in a sql statement
* [escapeValue](#ormentitymanagerescapevalue) Returns $value formatted to use in a sql statement.
* [fetch](#ormentitymanagerfetch) Fetch one or more entities
Expand Down Expand Up @@ -6862,21 +6862,22 @@ Returns whether or not an observer got detached.
#### ORM\EntityManager::eagerLoad

```php
public function eagerLoad( $relation, ORM\Entity $entities )
public function eagerLoad( string $relation, ORM\Entity $entities )
```

##### Load the $relation on all $entities with the least amount of queries


$relation can be nested by dividing them with a dot.

**Visibility:** this method is **public**.
<br />

**Throws:** this method may throw **\ORM\Exception\UndefinedRelation**<br />

##### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `$relation` | | |
| `$relation` | **string** | |
| `$entities` | **Entity** | |


Expand Down Expand Up @@ -7478,7 +7479,7 @@ At the end you should call finish bulk insert otherwise you may loose data.
* [delete](#ormtestingentitymanagermockdelete) Delete $entity from database
* [describe](#ormtestingentitymanagermockdescribe) Returns an array of columns from $table.
* [detach](#ormtestingentitymanagermockdetach) Detach $observer from all classes
* [eagerLoad](#ormtestingentitymanagermockeagerload)
* [eagerLoad](#ormtestingentitymanagermockeagerload) Load the $relation on all $entities with the least amount of queries
* [escapeIdentifier](#ormtestingentitymanagermockescapeidentifier) Returns $identifier quoted for use in a sql statement
* [escapeValue](#ormtestingentitymanagermockescapevalue) Returns $value formatted to use in a sql statement.
* [fetch](#ormtestingentitymanagermockfetch) Fetch one or more entities
Expand Down Expand Up @@ -7807,21 +7808,22 @@ Returns whether or not an observer got detached.
#### ORM\Testing\EntityManagerMock::eagerLoad

```php
public function eagerLoad( $relation, ORM\Entity $entities )
public function eagerLoad( string $relation, ORM\Entity $entities )
```

##### Load the $relation on all $entities with the least amount of queries


$relation can be nested by dividing them with a dot.

**Visibility:** this method is **public**.
<br />

**Throws:** this method may throw **\ORM\Exception\UndefinedRelation**<br />

##### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `$relation` | | |
| `$relation` | **string** | |
| `$entities` | **\ORM\Entity** | |


Expand Down
14 changes: 12 additions & 2 deletions src/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,15 @@ public function fetch($class, $primaryKey = null)
return $fetcher->one();
}

/**
* Load the $relation on all $entities with the least amount of queries
*
* $relation can be nested by dividing them with a dot.
*
* @param string $relation
* @param Entity ...$entities
* @throws Exception\UndefinedRelation
*/
public function eagerLoad($relation, Entity ...$entities)
{
$relations = explode('.', $relation); // split the relations by .
Expand All @@ -667,7 +676,8 @@ public function eagerLoad($relation, Entity ...$entities)
if (count($relations) > 0) {
// get all related objects of this relation
$entities = array_merge(...array_map(function ($relatedObject) use ($relation) {
return (array)$relatedObject->getRelated($relation);
$related = $relatedObject->getRelated($relation);
return !is_array($related) ? [$related] : $related;
}, $entities));
}
}
Expand All @@ -692,7 +702,7 @@ public function eagerLoad($relation, Entity ...$entities)
*/
public function observe($class, ObserverInterface $observer = null)
{
$returnObserver = $observer ? false : true;
$returnObserver = !$observer;
$observer || $observer = new CallbackObserver();

if (!isset($this->observers[$class])) {
Expand Down
28 changes: 27 additions & 1 deletion tests/EntityManager/EagerLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Mockery as m;
use ORM\Entity;
use ORM\Relation\ManyToMany;
use ORM\Relation\OneToOne;
use ORM\Test\Entity\Examples\Article;
use ORM\Test\Entity\Examples\User;
use ORM\Test\TestCase;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function loadsTheRelationWhenOneObjectHasNotLoadedTheRelation()
}

/** @test */
public function repeatsTheProcessForDeeperRelations()
public function repeatsTheProcessForNestedRelations()
{
$user = m::mock(User::class, ['id' => 1]);
$articles = [
Expand All @@ -83,4 +84,29 @@ public function repeatsTheProcessForDeeperRelations()

$this->em->eagerLoad('articles.categories', $user);
}

/** @test */
public function correctlyCollectsAllRelatedObjectsToAnArray()
{
$user1 = m::mock(User::class, ['id' => 1]);
$user2 = m::mock(User::class, ['id' => 2]);
$articles = [
m::mock(Article::class, ['id' => 1]),
m::mock(Article::class, ['id' => 2]),
m::mock(Article::class, ['id' => 3]),
];

foreach ($articles as $i => $article) {
$article->shouldReceive('hasLoaded')->with('writer')->once()->andReturnTrue();
$article->shouldReceive('getRelated')->with('writer')->once()->andReturn($i%2 === 0 ? $user1 : $user2);
}

$user1->shouldReceive('hasLoaded')->with('contact')->once()->andReturn(true);
$user2->shouldReceive('hasLoaded')->with('contact')->once()->andReturn(false);
$user1->shouldReceive('getRelation')->with('contact')->once()
->andReturn($relation = m::mock(OneToOne::class));
$relation->shouldReceive('eagerLoad')->with($this->em, $user1, $user2, $user1)->once();

$this->em->eagerLoad('writer.contact', ...$articles);
}
}

0 comments on commit e799aaa

Please sign in to comment.