Skip to content
Permalink
Browse files

Implementing ability to use a custom join type in contain

This helps overriding association settings without setting state
in the association clases.
  • Loading branch information...
lorenzo committed Sep 26, 2014
1 parent d5a189d commit 8be67566a530cd69841505a51788805e4e60281b
Showing with 28 additions and 2 deletions.
  1. +3 −1 src/ORM/Association.php
  2. +3 −1 src/ORM/EagerLoader.php
  3. +22 −0 tests/TestCase/ORM/QueryTest.php
@@ -432,6 +432,7 @@ protected function _options(array $options) {
* - propertyPath: A dot separated string representing the path of association
* properties to be followed from the passed query main entity to this
* association
* - joinType: The SQL join type to use in the query.
*
* @param Query $query the query to be altered to include the target table data
* @param array $options Any extra options or overrides to be taken in account
@@ -441,12 +442,13 @@ protected function _options(array $options) {
*/
public function attachTo(Query $query, array $options = []) {
$target = $this->target();
$joinType = empty($options['joinType']) ? $this->joinType() : $options['joinType'];
$options += [
'includeFields' => true,
'foreignKey' => $this->foreignKey(),
'conditions' => [],
'fields' => [],
'type' => empty($options['matching']) ? $this->joinType() : 'INNER',
'type' => empty($options['matching']) ? $joinType : 'INNER',
'table' => $target->table(),
'finder' => $this->finder()
];
@@ -58,7 +58,8 @@ class EagerLoader {
'sort' => 1,
'matching' => 1,
'queryBuilder' => 1,
'finder' => 1
'finder' => 1,
'joinType' => 1
];
/**
@@ -93,6 +94,7 @@ class EagerLoader {
* - queryBuilder: Equivalent to passing a callable instead of an options array
* - matching: Whether to inform the association class that it should filter the
* main query by the results fetched by that class.
* - joinType: For joinable associations, the SQL join type to use.
*
* @param array|string $associations list of table aliases to be queried.
* When this method is called multiple times it will merge previous list with
@@ -2152,4 +2152,26 @@ public function testContainFinderCanSpecifyOptions() {
$this->assertEquals($authorId, $resultWithAuthor->first()['author']['id']);
}
/**
* Tests that it is possible to pass a custom join type for an association when
* using contain
*
* @return void
*/
public function testContainWithCustomJoinType() {
$table = TableRegistry::get('Articles');
$table->belongsTo('Authors');
$articles = $table->find()
->contain([
'Authors' => [
'joinType' => 'inner',
'conditions' => ['Authors.id' => 3]
]
])
->toArray();
$this->assertCount(1, $articles);
$this->assertEquals(3, $articles[0]->author->id);
}
}

0 comments on commit 8be6756

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