Problem/Motivation
Using a database condition as fourth argument ($condition) of SelectQuery::addJoin() leads to a blank ON clause and a notice: Recoverable fatal error: Method DatabaseCondition::__toString() must return a string value in __toString() (line 1543 of /home/roman/code/www/camp/htdocs/includes/database/select.inc).
Without this feature it is very difficult to add advanced conditions (e.g., EXISTS) to a join.
Proposed resolution
Modify the Database/Query/Select class to check whether $condition is an object when compiling and executing a query, and recursively apply any processing to the object. (Essentially treat join conditions the same way as other possible objects, such as subquery tables.)
Remaining tasks
Create tests- Get feedback on patch
- Backport to d7
User interface changes
None
API changes
None
Data model changes
None
Steps to reproduce
Use the following snippet and execute it in a drupal environment (ie. use drush php-script).
$cond = db_and();
$cond->condition('n1.nid', 1);
$cond->condition('n2.nid', 1);
$query = db_select('node', 'n1');
$query->addJoin('INNER', 'node', 'n2', $cond);
print $query->__toString();
Expected result
SELECT
FROM
{node} n1
INNER JOIN {node} n2 ON (n1.nid = :db_condition_placeholder_0) AND (n2.nid = :db_condition_placeholder_1)
Actual result
SELECT
FROM
{node} n1
INNER JOIN {node} n2 ON
Additional observations
When the same condition is additionally passed as $query->condition both conditions work!
$cond = db_and();
$cond->condition('n1.nid', 1);
$cond->condition('n2.nid', 1);
$query = db_select('node', 'n1');
$query->addJoin('INNER', 'node', 'n2', $cond);
$query->condition($cond);
print $query->__toString();
leads to
SELECT
FROM
{node} n1
INNER JOIN {node} n2 ON (n1.nid = :db_condition_placeholder_0) AND (n2.nid = :db_condition_placeholder_1)
WHERE ( (n1.nid = :db_condition_placeholder_0) AND (n2.nid = :db_condition_placeholder_1) )