Browse Source

customConfig overwrites scopeFinds config with respect to all fields.

redd 11 years ago
parent
commit
4cfa0adc3f

+ 9 - 1
Model/Behavior/NamedScopeBehavior.php

@@ -132,7 +132,15 @@ class NamedScopeBehavior extends ModelBehavior {
 		}
 
 		$config = $finds[$key];
-		$config['find'] = array_merge_recursive($config['find'], $customConfig);
+		foreach ($customConfig as $keyCustom => $custom) {
+			if ($keyCustom === 'options') {
+				foreach ($custom as $keyOptions => $option) {
+					$config['find']['options'][$keyOptions] = $option;
+				}
+			} else {
+				$config['find'][$keyCustom] = $custom;
+			}
+		}
 		if (!isset($config['find']['type'])) {
 			$config['find']['type'] = 'all';
 		}

+ 44 - 0
Test/Case/Model/Behavior/NamedScopeBehaviorTest.php

@@ -212,6 +212,50 @@ class NamedScopeBehaviorTest extends MyCakeTestCase {
 	}
 
 	/**
+	 * NamedScopeBehaviorTest::testScopedFindOverwrite()
+	 *
+	 * @return void
+	 */
+	public function testScopedFindOverwrite() {
+		$this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
+
+		$this->Comment->Behaviors->load('Tools.NamedScope');
+		$this->Comment->User->Behaviors->load('Tools.NamedScope');
+		$this->Comment->scopedFinds = array(
+			'active' => array(
+				'name' => 'Active Comentators',
+				'find' => array(
+					'type' => 'all',
+					'virtualFields' => array(
+						'fullname' => "CONCAT(User.id, '-', User.user)"
+					),
+					'options' => array(
+						'scope' => array('Comment.active'),
+						'contain' => array('User'),
+						'fields' => array('User.id', 'fullname'),
+						'order' => array('fullname' => 'ASC'),
+						'limit' => 5
+					)
+				)
+			)
+		);
+		$result = $this->Comment->scopedFind('active', array('options' => array('limit' => 2)));
+		$this->assertSame(2, count($result));
+
+		$result = $this->Comment->scopedFind('active', array('type' => 'count'));
+		$this->assertSame(5, $result);
+
+		$result = $this->Comment->scopedFind('active', array('type' => 'first', 'options' => array('fields' => array('User.id', 'User.created'), 'order' => array('User.id' => 'DESC'))));
+		$expected = array(
+			'User' => array(
+				'id' => 4,
+				'created' => '2007-03-17 01:22:23'
+			)
+		);
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
 	 * NamedScopeBehaviorTest::testException()
 	 *
 	 * @expectedException RuntimeException