Skip to content
Permalink
Browse files

Avoid unsetting Model's $findMethods when defining a custom find meth…

…od in AppModel, also add tests for custom finds
  • Loading branch information...
ceeram committed Mar 3, 2012
1 parent acccdcd commit 08a4d0bf63d8c095e4a967c52503379a3d3568c3
Showing with 92 additions and 0 deletions.
  1. +2 −0 lib/Cake/Model/Model.php
  2. +20 −0 lib/Cake/Test/Case/Model/ModelReadTest.php
  3. +70 −0 lib/Cake/Test/Case/Model/models.php
@@ -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) {
@@ -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));
}
}
@@ -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
@@ -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.
You can’t perform that action at this time.