Skip to content

Commit 1c0cd9e

Browse files
authored
Merge pull request jeremyharris#27 from fabiofdsantos/master
Avoid lazy load if the association is empty
2 parents 1319c1f + fdfff9f commit 1c0cd9e

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/ORM/LazyLoadEntityTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected function _lazyLoad($property)
128128
->associations()
129129
->getByProperty($property);
130130

131-
if ($association === null) {
131+
if ($association === null || $this->get($association->getBindingKey()) === null) {
132132
return null;
133133
}
134134

tests/TestCase/ORM/LazyLoadEntityTraitTest.php

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace JeremyHarris\LazyLoad\Test\TestCase\ORM;
33

4+
use Cake\Datasource\ConnectionManager;
45
use Cake\Datasource\EntityInterface;
6+
use Cake\Log\Log;
57
use Cake\ORM\Entity;
68
use Cake\ORM\Locator\LocatorAwareTrait;
79
use Cake\TestSuite\TestCase;
@@ -17,7 +19,6 @@
1719
*/
1820
class LazyLoadEntityTraitTest extends TestCase
1921
{
20-
2122
use LocatorAwareTrait;
2223

2324
/**
@@ -43,6 +44,8 @@ public function setUp(): void
4344
{
4445
parent::setUp();
4546

47+
$this->connection = ConnectionManager::get('test');
48+
4649
$this->Articles = $this->getTableLocator()->get('Articles');
4750
$this->Articles->setEntityClass(LazyLoadableEntity::class);
4851
$this->Articles->belongsTo('Authors');
@@ -97,6 +100,35 @@ public function testMissingPrimaryKey()
97100
$this->assertNull($comment->author);
98101
}
99102

103+
/**
104+
* tests that no db queries are performed if there's no association
105+
*
106+
* @return void
107+
*/
108+
public function testNoDbQueriesOnEmptyAssociation()
109+
{
110+
Log::setConfig('queries', ['className' => 'Array']);
111+
$log = Log::engine('queries');
112+
113+
$this->connection->enableQueryLogging();
114+
115+
$article = $this->Articles->newEntity([
116+
'title' => 'Article with no author',
117+
'body' => 'Article content',
118+
]);
119+
120+
// Force a database query
121+
$this->getTableLocator()->get('Authors')->getSchema();
122+
123+
$initialQueries = count($log->read());
124+
$this->assertFalse(isset($article->author));
125+
$finalQueries = count($log->read());
126+
127+
$this->connection->disableQueryLogging();
128+
129+
$this->assertEquals($initialQueries, $finalQueries);
130+
}
131+
100132
/**
101133
* tests that we can override _repository to prevent errors from being thrown
102134
* in cases where we're creating an entity without a table. this happens in
@@ -166,7 +198,7 @@ public function testUnsetProperty()
166198
->will($this->returnValue($this->Comments));
167199

168200
$this->assertInstanceOf(EntityInterface::class, $comment->author);
169-
$comment->unsetProperty('author');
201+
$comment->unset('author');
170202
$this->assertNull($comment->author);
171203

172204
// test re-setting a previously un-set prop
@@ -225,7 +257,7 @@ public function testUnsetEagerLoadedProperty()
225257
->first();
226258

227259
$this->assertInstanceOf(EntityInterface::class, $comment->author);
228-
$comment->unsetProperty('author');
260+
$comment->unset('author');
229261
$this->assertNull($comment->author);
230262
}
231263

0 commit comments

Comments
 (0)