File tree 3 files changed +12
-5
lines changed
3 files changed +12
-5
lines changed Original file line number Diff line number Diff line change 2
2
namespace JeremyHarris \LazyLoad \ORM ;
3
3
4
4
use Cake \Datasource \RepositoryInterface ;
5
+ use Cake \ORM \Association \BelongsTo ;
5
6
use Cake \ORM \Entity ;
6
7
use Cake \ORM \Table ;
7
8
use Cake \ORM \TableRegistry ;
@@ -128,7 +129,10 @@ protected function _lazyLoad($property)
128
129
->associations ()
129
130
->getByProperty ($ property );
130
131
131
- if ($ association === null || $ this ->get ($ association ->getBindingKey ()) === null ) {
132
+ // is belongsTo and missing FK on this table? loadInto tries to load belongsTo data regardless
133
+ $ isMissingBelongsToFK = $ association instanceof BelongsTo && !isset ($ this ->_fields [$ association ->getForeignKey ()]);
134
+
135
+ if ($ association === null || $ isMissingBelongsToFK ) {
132
136
return null ;
133
137
}
134
138
Original file line number Diff line number Diff line change @@ -29,9 +29,9 @@ class AuthorsFixture extends TestFixture
29
29
* @var array
30
30
*/
31
31
public $ fields = [
32
- 'id ' => ['type ' => 'integer ' ],
32
+ 'author_id ' => ['type ' => 'integer ' ],
33
33
'name ' => ['type ' => 'string ' , 'default ' => null ],
34
- '_constraints ' => ['primary ' => ['type ' => 'primary ' , 'columns ' => ['id ' ]]]
34
+ '_constraints ' => ['primary ' => ['type ' => 'primary ' , 'columns ' => ['author_id ' ]]]
35
35
];
36
36
37
37
/**
Original file line number Diff line number Diff line change @@ -46,6 +46,9 @@ public function setUp(): void
46
46
47
47
$ this ->connection = ConnectionManager::get ('test ' );
48
48
49
+ $ this ->Authors = $ this ->getTableLocator ()->get ('Authors ' );
50
+ $ this ->Authors ->setPrimaryKey ('author_id ' );
51
+
49
52
$ this ->Articles = $ this ->getTableLocator ()->get ('Articles ' );
50
53
$ this ->Articles ->setEntityClass (LazyLoadableEntity::class);
51
54
$ this ->Articles ->belongsTo ('Authors ' );
@@ -313,7 +316,7 @@ public function testGetLazyLoadsOnce()
313
316
314
317
$ author = $ comment ->author ;
315
318
316
- $ this ->assertEquals (2 , $ author ->id );
319
+ $ this ->assertEquals (2 , $ author ->author_id );
317
320
318
321
// ensure it is grabbed from _properties and not lazy loaded again (which calls repository())
319
322
$ comment ->author ;
@@ -375,7 +378,7 @@ public function testEmptySource()
375
378
$ comment = new Comment (['id ' => 1 , 'user_id ' => 2 ]);
376
379
$ author = $ comment ->author ;
377
380
378
- $ this ->assertEquals (2 , $ author ->id );
381
+ $ this ->assertEquals (2 , $ author ->author_id );
379
382
}
380
383
381
384
/**
You can’t perform that action at this time.
0 commit comments