Skip to content

Commit

Permalink
Using th edefault finder method in Association
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 22, 2014
1 parent fb45363 commit 94fef9f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ORM/Association.php
Expand Up @@ -515,7 +515,8 @@ public function defaultRowValue($row, $joined) {
* @see \Cake\ORM\Table::find()
* @return \Cake\ORM\Query
*/
public function find($type = 'all', array $options = []) {
public function find($type = null, array $options = []) {
$type = $type ?: $this->finder();
return $this->target()
->find($type, $options)
->where($this->conditions());
Expand Down
43 changes: 43 additions & 0 deletions tests/TestCase/ORM/AssociationTest.php
Expand Up @@ -24,6 +24,14 @@
*/
class TestTable extends Table {

public function initialize(array $config = []) {
$this->schema(['id' => ['type' => 'integer']]);
}

public function findPublished($query) {
return $query->applyOptions(['this' => 'worked']);
}

}

/**
Expand Down Expand Up @@ -197,4 +205,39 @@ public function testInvalidStrategy() {
$this->assertEquals('subquery', $this->association->strategy());
}

public function testFinderMethod() {
$this->assertEquals('all', $this->association->finder());
$this->assertEquals('published', $this->association->finder('published'));
$this->assertEquals('published', $this->association->finder());
}

public function testFinderInConstructor() {
$config = [
'className' => '\Cake\Test\TestCase\ORM\TestTable',
'foreignKey' => 'a_key',
'conditions' => ['field' => 'value'],
'dependent' => true,
'sourceTable' => $this->source,
'joinType' => 'INNER',
'finder' => 'published'
];
$assoc = $this->getMock(
'\Cake\ORM\Association',
[
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'saveAssociated', 'eagerLoader', 'type'
],
['Foo', $config]
);
$this->assertEquals('published', $assoc->finder());
}

public function testCustomFinderIsUsed() {
$this->association->finder('published');
$this->assertEquals(
['this' => 'worked'],
$this->association->find()->getOptions()
);
}

}

0 comments on commit 94fef9f

Please sign in to comment.