Permalink
Browse files

All tests passing again

  • Loading branch information...
1 parent 568c702 commit 55562705a8d12a00e8565027796ac2100c43891e @lorenzo lorenzo committed Feb 5, 2014
@@ -415,7 +415,14 @@ public function attachTo(Query $query, array $options = []) {
}
$this->_dispatchBeforeFind($dummy);
- $this->_copyAttributes($query, $dummy, $options);
+
+ $joinOptions = ['table' => 1, 'conditions' => 1, 'type' => 1];
+ $options['conditions']->add($dummy->clause('where') ?: []);
+ $query->join([$target->alias() => array_intersect_key($options, $joinOptions)]);
+
+ $this->_appendFields($query, $dummy, $options);
+ $this->_formatAssociationResults($query, $dummy);
+ $this->_bindNewAssociations($query, $dummy, $options);
}
/**
@@ -474,13 +481,9 @@ protected function _dispatchBeforeFind($query) {
$table->getEventManager()->dispatch($event);
}
- protected function _copyAttributes($query, $surrogate, $options) {
- $joinOptions = ['table' => 1, 'conditions' => 1, 'type' => 1];
- $options['conditions']->add($surrogate->clause('where') ?: []);
- $target = $this->_targetTable;
- $query->join([$target->alias() => array_intersect_key($options, $joinOptions)]);
-
+ protected function _appendFields($query, $surrogate, $options) {
$options['fields'] = $surrogate->clause('select') ?: $options['fields'];
+ $target = $this->_targetTable;
if (empty($options['fields'])) {
$f = isset($options['fields']) ? $options['fields'] : null;
if ($options['includeFields'] && ($f === null || $f !== false)) {
@@ -491,13 +494,9 @@ protected function _copyAttributes($query, $surrogate, $options) {
if (!empty($options['fields'])) {
$query->select($query->aliasFields($options['fields'], $target->alias()));
}
-
-
- $this->_formatAssociationResults($query, $surrogate, $options);
- $this->_bindNewAssociations($query, $surrogate, $options);
}
- protected function _formatAssociationResults($query, $surrogate, $options) {
+ protected function _formatAssociationResults($query, $surrogate) {
$formatters = $surrogate->formatResults();
if (!$formatters) {
@@ -517,15 +516,18 @@ protected function _formatAssociationResults($query, $surrogate, $options) {
protected function _bindNewAssociations($query, $surrogate, $options) {
$contain = $surrogate->contain();
$target = $this->_targetTable;
- if ($contain) {
- $loader = $surrogate->eagerLoader();
- $loader->attachAssociations($query, $target, $options['includeFields']);
- $newBinds = [];
- foreach ($contain as $alias => $value) {
- $newBinds[$options['path'] . '.' . $alias] = $value;
- }
- $query->contain($newBinds);
+
+ if (!$contain) {
+ return;
+ }
+
+ $loader = $surrogate->eagerLoader();
+ $loader->attachAssociations($query, $target, $options['includeFields']);
+ $newBinds = [];
+ foreach ($contain as $alias => $value) {
+ $newBinds[$options['path'] . '.' . $alias] = $value;
}
+ $query->contain($newBinds);
}
/**
@@ -1479,15 +1479,23 @@ public function testAttachToBeforeFind() {
$association = new BelongsToMany('Tags', $config);
$listener = $this->getMock('stdClass', ['__invoke']);
$this->tag->getEventManager()->attach($listener, 'Model.beforeFind');
- $newQuery = $this->tag->query();
$listener->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $newQuery, [], false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ [],
+ false
+ );
$listener2 = $this->getMock('stdClass', ['__invoke']);
$table->getEventManager()->attach($listener2, 'Model.beforeFind');
- $newQuery2 = $table->query();
$listener2->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $newQuery2, [], false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ [],
+ false
+ );
$association->attachTo($query);
}
@@ -1509,15 +1517,23 @@ public function testAttachToBeforeFindExtraOptions() {
$listener = $this->getMock('stdClass', ['__invoke']);
$this->tag->getEventManager()->attach($listener, 'Model.beforeFind');
$opts = ['something' => 'more'];
- $newQuery = $this->tag->query()->applyOptions($opts);
$listener->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $newQuery, $opts, false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ $opts,
+ false
+ );
$listener2 = $this->getMock('stdClass', ['__invoke']);
$table->getEventManager()->attach($listener2, 'Model.beforeFind');
- $newQuery2 = $table->query();
$listener2->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $newQuery2, [], false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ [],
+ false
+ );
$association->attachTo($query, ['queryBuilder' => function($q) {
return $q->applyOptions(['something' => 'more']);
@@ -344,9 +344,13 @@ public function testAttachToBeforeFind() {
$listener = $this->getMock('stdClass', ['__invoke']);
$this->company->getEventManager()->attach($listener, 'Model.beforeFind');
$association = new BelongsTo('Companies', $config);
- $dummy = $this->company->query();
$listener->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $dummy, [], false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ [],
+ false
+ );
$association->attachTo($query);
}
@@ -367,9 +371,13 @@ public function testAttachToBeforeFindExtraOptions() {
$this->company->getEventManager()->attach($listener, 'Model.beforeFind');
$association = new BelongsTo('Companies', $config);
$options = ['something' => 'more'];
- $dummy = $this->company->query()->applyOptions($options);
$listener->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $dummy, $options, false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ $options,
+ false
+ );
$association->attachTo($query, ['queryBuilder' => function($q) {
return $q->applyOptions(['something' => 'more']);
}]);
@@ -788,9 +788,13 @@ public function testAttachToBeforeFind() {
$listener = $this->getMock('stdClass', ['__invoke']);
$association = new HasMany('Articles', $config);
$this->article->getEventManager()->attach($listener, 'Model.beforeFind');
- $dummy = $this->article->query();
$listener->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $dummy, [], false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ [],
+ false
+ );
$association->attachTo($query);
}
@@ -811,9 +815,13 @@ public function testAttachToBeforeFindExtraOptions() {
$association = new HasMany('Articles', $config);
$this->article->getEventManager()->attach($listener, 'Model.beforeFind');
$opts = ['something' => 'more'];
- $dummy = $this->article->query()->applyOptions($opts);
$listener->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $dummy, $opts, false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ $opts,
+ false
+ );
$association->attachTo($query, ['queryBuilder' => function($q) {
return $q->applyOptions(['something' => 'more']);
}]);
@@ -320,10 +320,14 @@ public function testAttachToBeforeFind() {
];
$listener = $this->getMock('stdClass', ['__invoke']);
$this->profile->getEventManager()->attach($listener, 'Model.beforeFind');
- $dummy = $this->profile->query();
$association = new HasOne('Profiles', $config);
$listener->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $dummy, [], false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ [],
+ false
+ );
$association->attachTo($query);
}
@@ -344,9 +348,13 @@ public function testAttachToBeforeFindExtraOptions() {
$this->profile->getEventManager()->attach($listener, 'Model.beforeFind');
$association = new HasOne('Profiles', $config);
$opts = ['something' => 'more'];
- $dummy = $this->profile->query()->applyOptions($opts);
$listener->expects($this->once())->method('__invoke')
- ->with($this->isInstanceOf('\Cake\Event\Event'), $dummy, $opts, false);
+ ->with(
+ $this->isInstanceOf('\Cake\Event\Event'),
+ $this->isInstanceOf('\Cake\ORM\Query'),
+ $opts,
+ false
+ );
$association->attachTo($query, ['queryBuilder' => function($q) {
return $q->applyOptions(['something' => 'more']);
}]);

0 comments on commit 5556270

Please sign in to comment.