Skip to content

Commit

Permalink
Implementing ability to use a custom join type in contain
Browse files Browse the repository at this point in the history
This helps overriding association settings without setting state
in the association clases.
  • Loading branch information
lorenzo committed Sep 26, 2014
1 parent d5a189d commit 8be6756
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ORM/Association.php
Expand Up @@ -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
Expand All @@ -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()
];
Expand Down
4 changes: 3 additions & 1 deletion src/ORM/EagerLoader.php
Expand Up @@ -58,7 +58,8 @@ class EagerLoader {
'sort' => 1,
'matching' => 1,
'queryBuilder' => 1,
'finder' => 1
'finder' => 1,
'joinType' => 1
];

/**
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -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.