Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented innerJoinWith()
  • Loading branch information
lorenzo committed Jun 4, 2015
1 parent 0949911 commit 09a0589
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/ORM/Query.php
Expand Up @@ -400,6 +400,40 @@ public function leftJoinWith($assoc, callable $builder = null) {
return $this;
}

/**
* Creates an INNER JOIN with the passed association table while preserving
* the foreign key matching and the custom conditions that were originally set
* for it.
*
* This function will add entries in the `contain` graph.
*
* ### Example:
*
* ```
* // Bring only articles that were tagged with 'cake'
* $query->innerJoinWith('Tags', function ($q) {
* return $q->where(['name' => 'cake']);
* );
* ```
*
* This function works the same as `matching()` with the difference that it
* will select no fields from the association.
*
* @param string $assoc The association to join with
* @param callable $builder a function that will receive a pre-made query object
* that can be used to add custom conditions or selecting some fields
* @return $this
* @see \Cake\ORM\Query::matching()
*/
public function innerJoinWith($assoc, callable $builder = null) {
$this->eagerLoader()->matching($assoc, $builder, [
'joinType' => 'INNER',
'fields' => false
]);
$this->_dirty();
return $this;
}

/**
* Returns a key => value array representing a single aliased field
* that can be passed directly to the select() method.
Expand Down
71 changes: 71 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -2768,4 +2768,75 @@ public function testLeftJoinWithSelect()
);
$this->assertNull($results->last()->_matchingData['articles']->id);
}

/**
* Tests innerJoinWith()
*
* @return void
*/
public function testInnerJoinWith()
{
$table = TableRegistry::get('authors');
$table->hasMany('articles');
$results = $table
->find()
->innerJoinWith('articles', function ($q) {
return $q->where(['articles.title' => 'Third Article']);
});
$expected = [
[
'id' => 1,
'name' => 'mariano'
]
];
$this->assertEquals($expected, $results->hydrate(false)->toArray());
}

/**
* Tests innerJoinWith() with nested associations
*
* @return void
*/
public function testInnerJoinWithNested()
{
$table = TableRegistry::get('authors');
$articles = $table->hasMany('articles');
$articles->belongsToMany('tags');
$results = $table
->find()
->innerJoinWith('articles.tags', function ($q) {
return $q->where(['tags.name' => 'tag3']);
});
$expected = [
[
'id' => 3,
'name' => 'larry'
]
];
$this->assertEquals($expected, $results->hydrate(false)->toArray());
}

/**
* Tests innerJoinWith() with select
*
* @return void
*/
public function testInnerJoinWithSelect()
{
$table = TableRegistry::get('authors');
$table->hasMany('articles');
$results = $table
->find()
->autoFields(true)
->innerJoinWith('articles', function ($q) {
return $q->select(['id', 'author_id', 'title', 'body', 'published']);
})
->toArray();

$expected = $table
->find()
->matching('articles')
->toArray();
$this->assertEquals($expected, $results);
}
}

0 comments on commit 09a0589

Please sign in to comment.