Skip to content
Permalink
Browse files

Make sure that the missing fields checks is done after the query is b…

…uilt

After this changes, when using the query builder function for a HasMany or
BelongsToMany association, failing to select the foreingKey fields will
actually trigger the helpful exception for the user.

Fixes #6606
  • Loading branch information...
lorenzo committed Mar 5, 2016
1 parent 88e1bb8 commit c7af5e85d5adf117f47a751043afa204206c382f
@@ -82,8 +82,11 @@ protected function _buildQuery($options)
$finder = isset($options['finder']) ? $options['finder'] : $this->finder();
list($finder, $opts) = $this->_extractFinder($finder);
$options += ['fields' => []];
$fetchQuery = $this
->find($finder, $opts)
->select($options['fields'])
->where($options['conditions'])
->eagerLoaded(true)
->hydrate($options['query']->hydrate());
@@ -95,16 +98,6 @@ protected function _buildQuery($options)
$fetchQuery = $this->_addFilteringCondition($fetchQuery, $key, $filter);
}
if (!empty($options['fields'])) {
$fields = $fetchQuery->aliasFields($options['fields'], $alias);
if (!in_array($key, $fields)) {
throw new InvalidArgumentException(
sprintf('You are required to select the "%s" field', $key)
);
}
$fetchQuery->select($fields);
}
if (!empty($options['sort'])) {
$fetchQuery->order($options['sort']);
}
@@ -117,6 +110,17 @@ protected function _buildQuery($options)
$fetchQuery = $options['queryBuilder']($fetchQuery);
}
$select = $fetchQuery->clause('select');
if (!empty($select)) {
$select = $fetchQuery->aliasFields($select);
if (array_diff((array)$key, $select) !== []) {
throw new InvalidArgumentException(
sprintf('You are required to select the "%s" field(s)',
implode(', ', (array)$key)
));
}
}
return $fetchQuery;
}
@@ -267,7 +267,7 @@ public function testEagerLoaderWithOverrides()
'conditions' => ['Articles.id !=' => 3],
'sort' => ['title' => 'DESC'],
'fields' => ['title', 'author_id'],
'contain' => ['Comments' => ['fields' => ['comment']]],
'contain' => ['Comments' => ['fields' => ['comment', 'article_id']]],
'keys' => $keys,
'query' => $query
]);
@@ -87,7 +87,7 @@ public function testEagerLoadingFromEmptyResults()
/**
* Tests that eagerloading belongsToMany with find list fails with a helpful message.
*
* @expectedException \RuntimeException
* @expectedException \InvalidArgumentException
* @return void
*/
public function testEagerLoadingBelongsToManyList()

0 comments on commit c7af5e8

Please sign in to comment.
You can’t perform that action at this time.