Skip to content
Snippets Groups Projects
Commit fb582e4e authored by Peter Rotich's avatar Peter Rotich
Browse files

ORM: Extra Joins

This commits adds ability to add extra joins to a queryset. If the join has
constraints then it's LEFT joined otherwise straight JOIN is assumed.

This is necessary to fix queue counts for queues with keyword search.
parent 8481d77f
No related branches found
No related tags found
No related merge requests found
......@@ -1287,6 +1287,10 @@ class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countabl
return $this;
}
function addExtraJoin(array $join) {
return $this->extra(array('joins' => array($join)));
}
function distinct() {
foreach (func_get_args() as $D)
$this->distinct[] = $D;
......@@ -2633,6 +2637,18 @@ class SqlCompiler {
$sql .= $join.$S;
}
}
// Add extra joins from QuerySet
if (isset($queryset->extra['joins'])) {
foreach ($queryset->extra['joins'] as $J) {
list($base, $constraints, $alias) = $J;
$join = $constraints ? ' LEFT JOIN ' : ' JOIN ';
$sql .= "{$join}{$base} $alias";
if ($constraints instanceof Q)
$sql .= ' ON ('.$this->compileQ($constraints, $queryset->model).')';
}
}
return $sql;
}
......
......@@ -902,6 +902,15 @@ class SavedQueue extends CustomQueue {
$query->aggregate(array(
"q{$queue->id}" => SqlAggregate::COUNT($expr, true)
));
// Add extra tables joins (if any)
if ($Q->extra && isset($Q->extra['tables'])) {
$contraints = array();
if ($Q->constraints)
$constraints = new Q($Q->constraints);
foreach ($Q->extra['tables'] as $T)
$query->addExtraJoin(array($T, $constraints, ''));
}
}
$counts = $query->values()->one();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment