Skip to content
Permalink
Browse files

Add ability to remove a defined join.

With BelongsToMany::find() appending joins, we'll need a way to redefine
and re-order those joins should the BelongsToMany association also be
eagerloaded/matched. Not being able to remove/re-order the joins results
in invalid SQL as the table ordering is wrong.

Refs #7707
  • Loading branch information...
markstory committed Dec 6, 2015
1 parent 8c2d4b2 commit f25eac3b46b9b3e23eb29e8e0e41ce6fdae29d36
Showing with 36 additions and 0 deletions.
  1. +16 −0 src/Database/Query.php
  2. +20 −0 tests/TestCase/Database/QueryTest.php
@@ -535,6 +535,22 @@ public function join($tables = null, $types = [], $overwrite = false)
return $this;
}
/**
* Remove a join if it has been defined.
*
* Useful when you are redefining joins or want to re-order
* the join clauses.
*
* @param string $name The alias/name of the join to remove.
* @return $this
*/
public function removeJoin($name)
{
unset($this->_parts['join'][$name]);
$this->_dirty();
return $this;
}
/**
* Adds a single LEFT JOIN clause to the query.
*
@@ -3499,6 +3499,26 @@ public function testDeepClone()
$this->assertNotEquals($query->clause('order'), $dupe->clause('order'));
}
/**
* Test removeJoin().
*
* @return void
*/
public function testRemoveJoin()
{
$query = new Query($this->connection);
$query->select(['id', 'title'])
->from('articles')
->join(['authors' => [
'type' => 'INNER',
'conditions' => ['articles.author_id = authors.id']
]]);
$this->assertArrayHasKey('authors', $query->join());
$this->assertSame($query, $query->removeJoin('authors'));
$this->assertArrayNotHasKey('authors', $query->join());
}
/**
* Assertion for comparing a table's contents with what is in it.
*

0 comments on commit f25eac3

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