Skip to main content
Version: Fegg 5.x (next)

Model::having

Set Having Parameter

Description​

public self Model::having( string $query, [ mixed $data = [], string $andOr = 'AND' ] )

Adds a HAVING condition, used with group. $data works as in where: one value, or an array of values in placeholder order. With having(), count counts the groups that match.

Arguments​

NameData typeDefaultRemarks
$querystringCondition with ? placeholders (never put user input in it)
$datamixed[]One value, or an array of values in placeholder order
$andOrstring'AND'How it joins the previous condition: AND or OR

Example​

Controller
$model = new Model(false, array(
'table' => 'cms_news',
'id' => 'news_id',
'visible' => 'visible',
));

// Categories with more than 5 news
// Select category, count(*) AS n From "cms_news" Where ... Group By category Having n > 5
$records = $model
->select('category, count(*) AS n')
->group('category')
->having('n > ?', 5)
->all();

Complex HAVING query​

Controller
$model = new Model(false, array(
'table' => 'cms_news',
'id' => 'news_id',
'visible' => 'visible',
));

$model->select('category, count(*) AS n, max(post_date) AS latest')->group('category');
$model->having('n > ?', 5);
$model->havingOpen();
$model->having('category = ?', 1);
$model->having('latest >= ?', '2026-01-01', 'OR');
$model->havingClose();

// ... Group By category
// Having n > 5 AND (category = 1 OR latest >= '2026-01-01')
$records = $model->all();