Skip to content
Permalink
Browse files

Reducing the ammount of duplicated code

  • Loading branch information...
lorenzo committed Jun 10, 2013
1 parent be7230d commit f909caef96019623462b9d8130610e3fd574ac32
@@ -209,49 +209,32 @@ public function eagerLoader(array $options) {
}
/**
* Auxiliary function to construct a new Query object to return all the records
* in the target table that are associated to those specified in $options from
* the source table
* Appends any conditions required to load the relevant set of records in the
* target table query given a filter key and some filtering values.
*
* @param array $options options accepted by eagerLoader()
* @return Cake\ORM\Query
* @param \Cake\ORM\Query taget table's query
* @param string $key the fields that should be used for filtering
* @param mixed $filter the value that should be used to match for $key
* @return \Cake\ORM\Query
*/
protected function _buildQuery($options) {
$target = $this->target();
$pivot = $this->pivot();
$alias = $target->alias();
$pivotAlias = $pivot->alias();
$options['conditions'] = array_merge($this->conditions(), $options['conditions']);
$key = sprintf('%s.%s', $pivotAlias, $options['foreignKey']);
$fetchQuery = $target->find('all')->where($options['conditions']);
$filter = ($options['strategy'] == parent::STRATEGY_SUBQUERY) ?
$this->_buildSubquery($options['query'], $key) : $options['keys'];
$options['contain'][$pivotAlias] = [
'conditions' => [$key . ' in' => $filter],
'matching' => true
];
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']);
}
if (!empty($options['contain'])) {
$fetchQuery->contain($options['contain']);
}
protected function _addFilteringCondition($query, $key, $filter) {
return $query->contain([
$this->pivot()->alias() => [
'conditions' => [$key . ' in' => $filter],
'matching' => true
]
]);
}
return $fetchQuery;
/**
* Generates a string used as a table field that contains the values upon
* which the filter should be applied
*
* params array $options
* @return string
*/
protected function _linkField($options) {
return sprintf('%s.%s', $this->pivot()->alias(), $options['foreignKey']);
}
/**
@@ -166,16 +166,14 @@ protected function _resultInjector($fetchQuery, $resultMap) {
protected function _buildQuery($options) {
$target = $this->target();
$alias = $target->alias();
$fetchQuery = $target->find('all');
$options['conditions'] = array_merge($this->conditions(), $options['conditions']);
$key = sprintf('%s.%s', $alias, $options['foreignKey']);
$key = $this->_linkField($options);
$filter = ($options['strategy'] == parent::STRATEGY_SUBQUERY) ?
$this->_buildSubquery($options['query'], $key) : $options['keys'];
$fetchQuery
->where($options['conditions'])
->andWhere([$key . ' in' => $filter]);
$fetchQuery = $target->find('all')->where($options['conditions']);
$fetchQuery = $this->_addFilteringCondition($fetchQuery, $key, $filter);
if (!empty($options['fields'])) {
$fields = $fetchQuery->aliasFields($options['fields'], $alias);
@@ -198,6 +196,30 @@ protected function _buildQuery($options) {
return $fetchQuery;
}
/**
* Appends any conditions required to load the relevant set of records in the
* target table query given a filter key and some filtering values.
*
* @param \Cake\ORM\Query taget table's query
* @param string $key the fields that should be used for filtering
* @param mixed $filter the value that should be used to match for $key
* @return \Cake\ORM\Query
*/
protected function _addFilteringCondition($query, $key, $filter) {
return $query->andWhere([$key . ' in' => $filter]);
}
/**
* Generates a string used as a table field that contains the values upon
* which the filter should be applied
*
* params array $options
* @return string
*/
protected function _linkField($options) {
return sprintf('%s.%s', $this->target()->alias(), $options['foreignKey']);
}
/**
* Builds a query to be used as a condition for filtering records in in the
* target table, it is constructed by cloning the original query that was used
@@ -271,12 +271,14 @@ public function testEagerLoader() {
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));
$query->expects($this->once())->method('contain')->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'matching' => true
]
]);
$query->expects($this->once())->method('contain')
->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'matching' => true
]
])
->will($this->returnSelf());
$callable = $association->eagerLoader(compact('keys'));
$row = ['Article__id' => 1, 'title' => 'article 1'];
@@ -326,12 +328,14 @@ public function testEagerLoaderWithDefaults() {
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));
$query->expects($this->once())->method('contain')->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'matching' => true
]
]);
$query->expects($this->once())->method('contain')
->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'matching' => true
]
])
->will($this->returnSelf());
$query->expects($this->once())->method('where')
->with(['Tag.name' => 'foo'])
@@ -376,12 +380,14 @@ public function testEagerLoaderWithOverrides() {
$query->expects($this->once())->method('execute')
->will($this->returnValue($results));
$query->expects($this->once())->method('contain')->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'matching' => true
]
]);
$query->expects($this->once())->method('contain')
->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $keys],
'matching' => true
]
])
->will($this->returnSelf());
$query->expects($this->once())->method('where')
->with([
@@ -436,6 +442,7 @@ public function testEagerLoaderFieldsException() {
$query = $this->getMock('Cake\ORM\Query', $methods, [null]);
$this->tag->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$query->expects($this->any())->method('contain')->will($this->returnSelf());
$query->expects($this->once())->method('where')->will($this->returnSelf());
@@ -500,12 +507,14 @@ public function testEagerLoaderSubquery() {
->with(['Tag.name' => 'foo'])
->will($this->returnValue($query));
$query->expects($this->once())->method('contain')->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $expected],
'matching' => true
]
]);
$query->expects($this->once())->method('contain')
->with([
'ArticleTag' => [
'conditions' => ['ArticleTag.article_id in' => $expected],
'matching' => true
]
])
->will($this->returnSelf());
$callable = $association->eagerLoader([
'query' => $parent, 'strategy' => BelongsToMany::STRATEGY_SUBQUERY

0 comments on commit f909cae

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