Skip to content

Commit

Permalink
Fixed Query::eagerLoaded() and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 19, 2014
1 parent 71ad141 commit d338a54
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
24 changes: 23 additions & 1 deletion src/Datasource/QueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ trait QueryTrait {
*/
protected $_options = [];

/**
* Whether the query is standalone or the product of an eager load operation.
*
* @var boolean
*/
protected $_eagerLoaded = false;

/**
* Returns the default table object that will be used by this query,
* that is, the table that will appear in the from clause.
Expand Down Expand Up @@ -168,6 +175,21 @@ public function cache($key, $config = 'default') {
return $this;
}

/**

This comment has been minimized.

Copy link
@ADmad

ADmad Mar 19, 2014

Member

I believe the correct grammar would be "..eagerly loaded query."

* Sets the query instance to be the eager loaded query. If no argument is
* passed, the current configured query `_eagerLoaded` value is returned.
*
* @param boolean $value
* @return \Cake\ORM\Query
*/
public function eagerLoaded($value = null) {
if ($value === null) {
return $this->_eagerLoaded;
}
$this->_eagerLoaded = $value;
return $this;
}

/**
* Fetch the results for this query.
*
Expand All @@ -185,7 +207,7 @@ public function all() {
}

$table = $this->repository();
$event = new Event('Model.beforeFind', $table, [$this, $this->_options, $this->eagerLoaded()]);
$event = new Event('Model.beforeFind', $table, [$this, $this->_options, !$this->eagerLoaded()]);
$table->getEventManager()->dispatch($event);

if (isset($this->_results)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public function attachTo(Query $query, array $options = []) {
}

$options['conditions'] = $query->newExpr()->add($options['conditions']);
$dummy = $target->query()->eagerLoaded(false);
$dummy = $target->query()->eagerLoaded(true);

if (!empty($options['queryBuilder'])) {
$dummy = $options['queryBuilder']($dummy);
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/ExternalAssociationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected function _buildQuery($options) {
$fetchQuery = $this
->find('all')
->where($options['conditions'])
->eagerLoaded(false)
->eagerLoaded(true)
->hydrate($options['query']->hydrate());
$fetchQuery = $this->_addFilteringCondition($fetchQuery, $key, $filter);

Expand Down
22 changes: 0 additions & 22 deletions src/ORM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ class Query extends DatabaseQuery {
*/
protected $_eagerLoader;

/**
* Whether the query is the primary query or a byproduct.
*
* @var boolean
*/
protected $_eagerLoaded = true;

/**
* Constuctor
*
Expand Down Expand Up @@ -653,21 +646,6 @@ protected function _dirty() {
parent::_dirty();
}

/**
* Sets the query instance to be the eager loaded query. If no argument is
* passed, the current configured query `_eagerLoaded` value is returned.
*
* @param boolean $value
* @return \Cake\ORM\Query
*/
public function eagerLoaded($value = null) {
if ($value === null) {
return $this->_eagerLoaded;
}
$this->_eagerLoaded = $value;
return $this;
}

/**
* Create an update query.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1815,4 +1815,30 @@ public function testDebugInfo() {
$this->assertSame($expected, $query->__debugInfo());
}

/**
* Tests that the eagerLoaded function works and is trnasmitted correctly to eagerly

This comment has been minimized.

Copy link
@ADmad

ADmad Mar 19, 2014

Member

Typo in "transmitted"

This comment has been minimized.

Copy link
@lorenzo

lorenzo Mar 19, 2014

Author Member

Fixed

* loaded associations
*
* @return void
*/
public function testEagerLoaded() {
$table = TableRegistry::get('authors');
$table->hasMany('articles');
$query = $table->find()->contain(['articles' => function($q) {
$this->assertTrue($q->eagerLoaded());
return $q;
}]);
$this->assertFalse($query->eagerLoaded());

$table->getEventManager()->attach(function($e, $q, $o, $primary) {
$this->assertTrue($primary);
}, 'Model.beforeFind');

TableRegistry::get('articles')
->getEventManager()->attach(function($e, $q, $o, $primary) {
$this->assertFalse($primary);
}, 'Model.beforeFind');
$query->all();
}

}

0 comments on commit d338a54

Please sign in to comment.