Browse Source

Making the internal representaition of containments a simple array, the
idea of returning the ArrayObject so it could be modified from the
outside was a terrible one, was not tested nor documented.

Jose Lorenzo Rodriguez 12 years ago
parent
commit
e40c150a0e
3 changed files with 12 additions and 22 deletions
  1. 4 5
      src/ORM/EagerLoader.php
  2. 3 12
      src/ORM/Query.php
  3. 5 5
      tests/TestCase/ORM/QueryTest.php

+ 4 - 5
src/ORM/EagerLoader.php

@@ -29,7 +29,7 @@ class EagerLoader {
  * Nested array describing the association to be fetched
  * and the options to apply for each of them, if any
  *
- * @var \ArrayObject
+ * @var array
  */
 	protected $_containments;
 
@@ -66,7 +66,7 @@ class EagerLoader {
 
 	public function contain($associations = null, $override = false) {
 		if ($this->_containments === null || $override) {
-			$this->_containments = new \ArrayObject;
+			$this->_containments = [];
 			$this->_normalized = null;
 		}
 
@@ -85,9 +85,8 @@ class EagerLoader {
 			return;
 		}
 
-		$old = $this->_containments->getArrayCopy();
-		$associations = $this->_reformatContain($associations, $old);
-		$this->_containments->exchangeArray($associations);
+		$associations = $this->_reformatContain($associations, $this->_containments);
+		$this->_containments = $associations;
 		$this->_normalized = null;
 	}
 

+ 3 - 12
src/ORM/Query.php

@@ -285,18 +285,9 @@ class Query extends DatabaseQuery {
  *		]
  *	]);
  *
- * If called with no arguments, this function will return an ArrayObject with
+ * If called with no arguments, this function will return an array with
  * with the list of previously configured associations to be contained in the
- * result. This object can be modified directly as the reference is kept inside
- * the query.
- *
- * The resulting ArrayObject will always have association aliases as keys, and
- * options as values, if no options are passed, the values will be set to an empty
- * array
- *
- * Please note that when modifying directly the containments array, you are
- * required to maintain the structure. That is, association names as keys
- * having array values. Failing to do so will result in an error
+ * result.
  *
  * If called with an empty first argument and $override is set to true, the
  * previous list will be emptied.
@@ -304,7 +295,7 @@ class Query extends DatabaseQuery {
  * @param array|string $associations list of table aliases to be queried
  * @param boolean $override whether override previous list with the one passed
  * defaults to merging previous list with the new one.
- * @return \ArrayObject|Query
+ * @return array|\Cake\ORM\Query
  */
 	public function contain($associations = null, $override = false) {
 		if (empty($associations) && $override) {

+ 5 - 5
tests/TestCase/ORM/QueryTest.php

@@ -205,7 +205,7 @@ class QueryTest extends TestCase {
 			'clients.orders.stuff',
 			'clients.companies.categories' => ['conditions' => ['a >' => 1]]
 		]);
-		$expected = new \ArrayObject([
+		$expected = [
 			'clients' => [
 				'orders' => [
 					'stuff' => []
@@ -216,7 +216,7 @@ class QueryTest extends TestCase {
 					]
 				]
 			]
-		]);
+		];
 		$this->assertEquals($expected, $query->contain());
 		$query->contain([
 			'clients.orders' => ['fields' => ['a', 'b']],
@@ -242,14 +242,14 @@ class QueryTest extends TestCase {
 			'clients' => $builder
 		]);
 
-		$expected = new \ArrayObject([
+		$expected = [
 			'clients' => [
 				'orders' => [
 					'stuff' => ['fields' => ['a']]
 				],
 				'queryBuilder' => $builder
 			]
-		]);
+		];
 		$this->assertEquals($expected, $query->contain());
 
 		$query = $this->getMock('\Cake\ORM\Query', ['join'], [$this->connection, $this->table]);
@@ -1011,7 +1011,7 @@ class QueryTest extends TestCase {
 		$expected = new QueryExpression($options['having']);
 		$this->assertEquals($expected, $query->clause('having'));
 
-		$expected = new \ArrayObject(['table_a' => ['table_b' => []]]);
+		$expected = ['table_a' => ['table_b' => []]];
 		$this->assertEquals($expected, $query->contain());
 	}