Skip to content
This repository
Browse code

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 authored March 03, 2012
2  lib/Cake/Model/Model.php
@@ -699,6 +699,8 @@ public function __construct($id = false, $table = null, $ds = null) {
699 699
 			}
700 700
 			$this->_mergeVars($merge, 'AppModel');
701 701
 		}
  702
+		$this->_mergeVars(array('findMethods'), 'Model');
  703
+
702 704
 		$this->Behaviors = new BehaviorCollection();
703 705
 
704 706
 		if ($this->useTable !== false) {
20  lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -7826,4 +7826,24 @@ public function testNotInArrayWithOneValue() {
7826 7826
 
7827 7827
 		$this->assertTrue(is_array($result) && !empty($result));
7828 7828
     }
  7829
+
  7830
+/**
  7831
+ * test custom find method
  7832
+ *
  7833
+ * @return void
  7834
+ */
  7835
+	public function testfindCustom() {
  7836
+		$this->loadFixtures('Article');
  7837
+		$Article = new CustomArticle();
  7838
+		$data = array('user_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  7839
+		$Article->create($data);
  7840
+		$Article->save();
  7841
+		$this->assertEquals(4, $Article->id);
  7842
+
  7843
+		$result = $Article->find('published');
  7844
+		$this->assertEquals(3, count($result));
  7845
+
  7846
+		$result = $Article->find('unPublished');
  7847
+		$this->assertEquals(1, count($result));
  7848
+    }
7829 7849
 }
70  lib/Cake/Test/Case/Model/models.php
@@ -18,6 +18,41 @@
18 18
  * @since         CakePHP(tm) v 1.2.0.6464
19 19
  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
20 20
  */
  21
+App::uses('Model', 'Model');
  22
+/**
  23
+ * AppModel class
  24
+ *
  25
+ * @package       Cake.Test.Case.Model
  26
+ */
  27
+class AppModel extends Model {
  28
+
  29
+/**
  30
+ * findMethods property
  31
+ *
  32
+ * @var array
  33
+ */
  34
+	public $findMethods = array('published' => true);
  35
+
  36
+/**
  37
+ * useDbConfig property
  38
+ *
  39
+ * @var array
  40
+ */
  41
+	public $useDbConfig = 'test';
  42
+
  43
+/**
  44
+ * _findPublished custom find
  45
+ *
  46
+ * @return array
  47
+ */
  48
+	public function _findPublished($state, $query, $results = array()) {
  49
+        if ($state === 'before') {
  50
+            $query['conditions']['published'] = 'Y';
  51
+            return $query;
  52
+        }
  53
+        return $results;
  54
+	}
  55
+}
21 56
 
22 57
 /**
23 58
  * Test class
@@ -4689,3 +4724,38 @@ class ArmorsPlayer extends CakeTestModel {
4689 4724
 	public $useDbConfig = 'test_database_three';
4690 4725
 
4691 4726
 }
  4727
+
  4728
+/**
  4729
+ * CustomArticle class
  4730
+ *
  4731
+ * @package       Cake.Test.Case.Model
  4732
+ */
  4733
+class CustomArticle extends AppModel {
  4734
+/**
  4735
+ * useTable property
  4736
+ *
  4737
+ * @var string
  4738
+ */
  4739
+	public $useTable = 'articles';
  4740
+
  4741
+/**
  4742
+ * findMethods property
  4743
+ *
  4744
+ * @var array
  4745
+ */
  4746
+	public $findMethods = array('unPublished' => true);
  4747
+
  4748
+/**
  4749
+ * _findUnPublished custom find
  4750
+ *
  4751
+ * @return array
  4752
+ */
  4753
+	public function _findUnPublished($state, $query, $results = array()) {
  4754
+        if ($state === 'before') {
  4755
+            $query['conditions']['published'] = 'N';
  4756
+            return $query;
  4757
+        }
  4758
+        return $results;
  4759
+	}
  4760
+
  4761
+}

0 notes on commit 08a4d0b

Please sign in to comment.
Something went wrong with that request. Please try again.