ソースを参照

adjust get() to be more 3.x

euromark 11 年 前
コミット
f895462f3c
3 ファイル変更29 行追加7 行削除
  1. 18 4
      Model/MyModel.php
  2. 3 0
      Test/Case/AllToolsTest.php
  3. 8 3
      Test/Case/Model/MyModelTest.php

+ 18 - 4
Model/MyModel.php

@@ -1440,11 +1440,11 @@ class MyModel extends Model {
 	 *   $record = $this->Model->get();
 	 *
 	 * @param mixed $id
-	 * @param string|array $fields
-	 * @param array $contain
+	 * @param array $options Options for find(). Used to be fields array/string.
+	 * @param array $contain Deprecated - use
 	 * @return mixed
 	 */
-	public function get($id = null, $fields = array(), $contain = array()) {
+	public function get($id = null, $options = array(), $contain = array()) {
 		if (is_array($id)) {
 			$column = $id[0];
 			$value = $id[1];
@@ -1459,6 +1459,17 @@ class MyModel extends Model {
 			return array();
 		}
 
+		// BC
+		$fields = null;
+		if (is_string($options)) {
+			$fields = $options;
+			$options = array();
+		}
+		if (!empty($options) && !array_key_exists('fields', $options) && !array_key_exists('contain', $options)) {
+			$fields = $options;
+			$options = array();
+		}
+
 		if ($fields === '*') {
 			$fields = $this->alias . '.*';
 		} elseif (!empty($fields)) {
@@ -1472,13 +1483,16 @@ class MyModel extends Model {
 
 		$options = array(
 			'conditions' => array($this->alias . '.' . $column => $value),
-		);
+		) + $options;
+
+		// BC
 		if (!empty($fields)) {
 			$options['fields'] = $fields;
 		}
 		if (!empty($contain)) {
 			$options['contain'] = $contain;
 		}
+
 		return $this->find('first', $options);
 	}
 

+ 3 - 0
Test/Case/AllToolsTest.php

@@ -44,6 +44,9 @@ class AllToolsTest extends PHPUnit_Framework_TestSuite {
 		$path = dirname(__FILE__);
 		$Suite->addTestDirectory($path . DS . 'Controller' . DS . 'Component' . DS . 'Auth');
 
+		//$path = dirname(__FILE__);
+		//$Suite->addTestDirectory($path . DS . 'Controller');
+
 		//$path = CakePlugin::path('Tools') . 'Test' . DS . 'Case' . DS;
 		//$Suite->addTestDirectoryRecursive($path);
 		return $Suite;

+ 8 - 3
Test/Case/Model/MyModelTest.php

@@ -16,7 +16,6 @@ class MyModelTest extends MyCakeTestCase {
 		parent::setUp();
 
 		$this->Post = ClassRegistry::init('MyAppModelPost');
-
 		$this->User = ClassRegistry::init('MyAppModelUser');
 	}
 
@@ -35,10 +34,16 @@ class MyModelTest extends MyCakeTestCase {
 		$record = $this->Post->get(2);
 		$this->assertEquals(2, $record['Post']['id']);
 
-		$record = $this->Post->get(2, array('fields' => 'id', 'created'));
+		$record = $this->Post->get(2, array('fields' => array('id', 'created')));
 		$this->assertEquals(2, count($record['Post']));
 
-		$record = $this->Post->get(2, array('fields' => 'id', 'title', 'body'), array('Author'));
+		$record = $this->Post->get(2, array('fields' => array('id', 'title', 'body'), 'contain' => array('Author')));
+		$this->assertEquals(3, count($record['Post']));
+		$this->assertEquals(3, $record['Author']['id']);
+
+		// BC
+		$record = $this->Post->get(2, array('id', 'title', 'body'), array('Author'));
+		$this->assertEquals(3, count($record['Post']));
 		$this->assertEquals(3, $record['Author']['id']);
 	}