Navigation Menu

Skip to content

Commit

Permalink
Making the internal representaition of containments a simple array, the
Browse files Browse the repository at this point in the history
idea of returning the ArrayObject so it could be modified from the
outside was a terrible one, was not tested nor documented.
  • Loading branch information
lorenzo committed Jan 30, 2014
1 parent 5664ab4 commit e40c150
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
9 changes: 4 additions & 5 deletions src/ORM/EagerLoader.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -85,9 +85,8 @@ public function contain($associations = null, $override = false) {
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;
}

Expand Down
15 changes: 3 additions & 12 deletions src/ORM/Query.php
Expand Up @@ -285,26 +285,17 @@ public function eagerLoader(EagerLoader $instance = null) {
* ]
* ]);
*
* 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.
*
* @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) {
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -205,7 +205,7 @@ public function testContainDotNotation() {
'clients.orders.stuff',
'clients.companies.categories' => ['conditions' => ['a >' => 1]]
]);
$expected = new \ArrayObject([
$expected = [
'clients' => [
'orders' => [
'stuff' => []
Expand All @@ -216,7 +216,7 @@ public function testContainDotNotation() {
]
]
]
]);
];
$this->assertEquals($expected, $query->contain());
$query->contain([
'clients.orders' => ['fields' => ['a', 'b']],
Expand All @@ -242,14 +242,14 @@ public function testContainClosure() {
'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]);
Expand Down Expand Up @@ -1011,7 +1011,7 @@ public function testApplyOptions() {
$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());
}

Expand Down

0 comments on commit e40c150

Please sign in to comment.