Skip to content

Commit

Permalink
Avoid unsetting Model's $findMethods when defining a custom find meth…
Browse files Browse the repository at this point in the history
…od in AppModel, also add tests for custom finds
  • Loading branch information
ceeram committed Mar 3, 2012
1 parent acccdcd commit 08a4d0b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Cake/Model/Model.php
Expand Up @@ -699,6 +699,8 @@ public function __construct($id = false, $table = null, $ds = null) {
}
$this->_mergeVars($merge, 'AppModel');
}
$this->_mergeVars(array('findMethods'), 'Model');

$this->Behaviors = new BehaviorCollection();

if ($this->useTable !== false) {
Expand Down
20 changes: 20 additions & 0 deletions lib/Cake/Test/Case/Model/ModelReadTest.php
Expand Up @@ -7826,4 +7826,24 @@ public function testNotInArrayWithOneValue() {

$this->assertTrue(is_array($result) && !empty($result));
}

/**
* test custom find method
*
* @return void
*/
public function testfindCustom() {
$this->loadFixtures('Article');
$Article = new CustomArticle();
$data = array('user_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
$Article->create($data);
$Article->save();
$this->assertEquals(4, $Article->id);

$result = $Article->find('published');
$this->assertEquals(3, count($result));

$result = $Article->find('unPublished');
$this->assertEquals(1, count($result));
}
}
70 changes: 70 additions & 0 deletions lib/Cake/Test/Case/Model/models.php
Expand Up @@ -18,6 +18,41 @@
* @since CakePHP(tm) v 1.2.0.6464
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Model', 'Model');
/**
* AppModel class
*
* @package Cake.Test.Case.Model
*/
class AppModel extends Model {

/**
* findMethods property
*
* @var array
*/
public $findMethods = array('published' => true);

/**
* useDbConfig property
*
* @var array
*/
public $useDbConfig = 'test';

/**
* _findPublished custom find
*
* @return array
*/
public function _findPublished($state, $query, $results = array()) {
if ($state === 'before') {
$query['conditions']['published'] = 'Y';
return $query;
}
return $results;
}
}

/**
* Test class
Expand Down Expand Up @@ -4689,3 +4724,38 @@ class ArmorsPlayer extends CakeTestModel {
public $useDbConfig = 'test_database_three';

}

/**
* CustomArticle class
*
* @package Cake.Test.Case.Model
*/
class CustomArticle extends AppModel {
/**
* useTable property
*
* @var string
*/
public $useTable = 'articles';

/**
* findMethods property
*
* @var array
*/
public $findMethods = array('unPublished' => true);

/**
* _findUnPublished custom find
*
* @return array
*/
public function _findUnPublished($state, $query, $results = array()) {
if ($state === 'before') {
$query['conditions']['published'] = 'N';
return $query;
}
return $results;
}

}

0 comments on commit 08a4d0b

Please sign in to comment.