Adding having() support to the Fluent query builder.
Signed-off-by: Ben Corlett <bencorlett@me.com>
This commit is contained in:
parent
589b86ff3e
commit
af6d76f713
|
@ -70,6 +70,13 @@ class Query {
|
|||
*/
|
||||
public $groupings;
|
||||
|
||||
/**
|
||||
* The HAVING clauses.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $havings;
|
||||
|
||||
/**
|
||||
* The ORDER BY clauses.
|
||||
*
|
||||
|
@ -475,6 +482,22 @@ public function group_by($column)
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a having to the query.
|
||||
*
|
||||
* @param string $column
|
||||
* @param string $operator
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function having($column, $operator, $value)
|
||||
{
|
||||
$this->havings[] = compact('column', 'operator', 'value');
|
||||
|
||||
$this->bindings[] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an ordering to the query.
|
||||
*
|
||||
|
|
|
@ -19,7 +19,7 @@ class Grammar extends \Laravel\Database\Grammar {
|
|||
*/
|
||||
protected $components = array(
|
||||
'aggregate', 'selects', 'from', 'joins', 'wheres',
|
||||
'groupings', 'orderings', 'limit', 'offset',
|
||||
'groupings', 'havings', 'orderings', 'limit', 'offset',
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -286,6 +286,24 @@ protected function groupings(Query $query)
|
|||
return 'GROUP BY '.$this->columnize($query->groupings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the HAVING clause for a query.
|
||||
*
|
||||
* @param Query $query
|
||||
* @return string
|
||||
*/
|
||||
protected function havings(Query $query)
|
||||
{
|
||||
if (is_null($query->havings)) return '';
|
||||
|
||||
foreach ($query->havings as $having)
|
||||
{
|
||||
$sql[] = 'AND '.$this->wrap($having['column']).' '.$having['operator'].' '.$this->parameter($having['value']);
|
||||
}
|
||||
|
||||
return 'HAVING '.preg_replace('/AND /', '', implode(' ', $sql), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the ORDER BY clause for a query.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue