Skip to content

Commit 94fef9f

Browse files
committed
Using th edefault finder method in Association
1 parent fb45363 commit 94fef9f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/ORM/Association.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ public function defaultRowValue($row, $joined) {
515515
* @see \Cake\ORM\Table::find()
516516
* @return \Cake\ORM\Query
517517
*/
518-
public function find($type = 'all', array $options = []) {
518+
public function find($type = null, array $options = []) {
519+
$type = $type ?: $this->finder();
519520
return $this->target()
520521
->find($type, $options)
521522
->where($this->conditions());

tests/TestCase/ORM/AssociationTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
*/
2525
class TestTable extends Table {
2626

27+
public function initialize(array $config = []) {
28+
$this->schema(['id' => ['type' => 'integer']]);
29+
}
30+
31+
public function findPublished($query) {
32+
return $query->applyOptions(['this' => 'worked']);
33+
}
34+
2735
}
2836

2937
/**
@@ -197,4 +205,39 @@ public function testInvalidStrategy() {
197205
$this->assertEquals('subquery', $this->association->strategy());
198206
}
199207

208+
public function testFinderMethod() {
209+
$this->assertEquals('all', $this->association->finder());
210+
$this->assertEquals('published', $this->association->finder('published'));
211+
$this->assertEquals('published', $this->association->finder());
212+
}
213+
214+
public function testFinderInConstructor() {
215+
$config = [
216+
'className' => '\Cake\Test\TestCase\ORM\TestTable',
217+
'foreignKey' => 'a_key',
218+
'conditions' => ['field' => 'value'],
219+
'dependent' => true,
220+
'sourceTable' => $this->source,
221+
'joinType' => 'INNER',
222+
'finder' => 'published'
223+
];
224+
$assoc = $this->getMock(
225+
'\Cake\ORM\Association',
226+
[
227+
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
228+
'saveAssociated', 'eagerLoader', 'type'
229+
],
230+
['Foo', $config]
231+
);
232+
$this->assertEquals('published', $assoc->finder());
233+
}
234+
235+
public function testCustomFinderIsUsed() {
236+
$this->association->finder('published');
237+
$this->assertEquals(
238+
['this' => 'worked'],
239+
$this->association->find()->getOptions()
240+
);
241+
}
242+
200243
}

0 commit comments

Comments
 (0)