Skip to content

Commit 7e1bcc1

Browse files
authored
Merge pull request #19 from themrwilliams/byref-property-fix
Fix issue where by-ref array functions didn't modify Entity properties.
2 parents e3fdd5f + 7b213bb commit 7e1bcc1

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/ORM/LazyLoadEntityTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ trait LazyLoadEntityTrait
3232
*/
3333
public function &get($property)
3434
{
35-
$get = $this->_parentGet($property);
35+
$get = &$this->_parentGet($property);
3636

3737
if ($get === null) {
3838
$get = $this->_lazyLoad($property);

tests/TestCase/ORM/LazyLoadEntityTraitTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
namespace JeremyHarris\LazyLoad\Test\TestCase\ORM;
33

44
use Cake\Datasource\EntityInterface;
5+
use Cake\ORM\Entity;
56
use Cake\ORM\TableRegistry;
67
use Cake\TestSuite\TestCase;
78
use JeremyHarris\LazyLoad\TestApp\Model\Entity\Comment;
89
use JeremyHarris\LazyLoad\TestApp\Model\Entity\LazyLoadableEntity;
910
use JeremyHarris\LazyLoad\TestApp\Model\Entity\TablelessEntity;
1011
use JeremyHarris\LazyLoad\TestApp\Model\Entity\User;
12+
use JeremyHarris\LazyLoad\TestApp\Model\Table\ArticlesTable;
1113

1214
/**
1315
* LazyLoadEntityTrait test
16+
* @property ArticlesTable $Articles
1417
*/
1518
class LazyLoadEntityTraitTest extends TestCase
1619
{
@@ -384,4 +387,32 @@ public function testDuplicateTrait()
384387
$user = $this->Users->get(1);
385388
$this->assertTrue($user->has('comments'));
386389
}
390+
391+
/**
392+
* @return void
393+
*/
394+
public function testByRefArrayFunctionsWorkOnNormalCakeEntities()
395+
{
396+
$this->Articles->setEntityClass(Entity::class);
397+
398+
$article = $this->Articles->get(1, ['contain' => ['Tags']]);
399+
$newTag = new Entity();
400+
401+
$this->assertCount(2, $article->tags);
402+
array_unshift($article->tags, $newTag);
403+
$this->assertCount(3, $article->tags);
404+
}
405+
406+
/**
407+
* @return void
408+
*/
409+
public function testByRefArrayFunctionsWorkOnLazyLoadCakeEntities()
410+
{
411+
$article = $this->Articles->get(1, ['contain' => ['Tags']]);
412+
$newTag = new Entity();
413+
414+
$this->assertCount(2, $article->tags);
415+
array_unshift($article->tags, $newTag);
416+
$this->assertCount(3, $article->tags);
417+
}
387418
}

0 commit comments

Comments
 (0)