Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 08a4d0bf63d8c095e4a967c52503379a3d3568c3 1 parent acccdcd
@ceeram ceeram authored
View
2  lib/Cake/Model/Model.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) {
View
20 lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -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));
+ }
}
View
70 lib/Cake/Test/Case/Model/models.php
@@ -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;
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.