Login +

Reference Guide / SLS_QueryBuilder

SLS_QueryBuilder1 is an utility class to create complex MySQL queries. It complements the searchModels() function of SLS_FrontModel2 class.

Accessibility

At Controller level, you can instantiate a new class:

$qbd = new SLS_QueryBuilder();

Handling

The following code examples show all the functions you can use to construct queries.

The expr() function of SLS_QueryBuilder1 class is a interface function with SLS_ExprBuilder3 class.

That this is the execute() function that will submit your request. So you can work continuously with your object before calling execute() function.

$qbd = new SLS_QueryBuilder();

// Select
$resu = $qbd->select('news_id','news_title','news_date')
            ->from('news')
            ->innerJoin('user', 'news.user_id = user.user_id')
            ->where($qbd->expr()->like('news_title','%e%'))
            ->whereOr($qbd->expr()->like('news_title','%a%'))
            ->groupBy("news_id")
            ->having($qbd->expr()->in('news_id',array(49,69,89,15)))
            ->order(array("news_title","news_date"),array("desc"))
            ->limit(1,10)
            ->execute();
# $resu should contain an array of PDO recordsets

// Update
$resu = $qbd->update()
            ->from('keyword')
            ->set($qbd->expr()->eq('keyword_name','updated'))
            ->where($qbd->expr()->eq('keyword_id','1'))
            ->execute();
# $resu should contain an int representing the number of recordsets updated

// Delete
$resu = $qbd->delete()
            ->from('keyword')                   
            ->where($qbd->expr()->eq('keyword_id','3'))
            ->execute();
# $resu should contain an int representing the number of recordsets deleted

References

  1. SLS_QueryBuilder - SLS_QueryBuilder class
  2. SLS_FrontModel - Mother class of models
  3. SLS_ExprBuilder - SLS_ExprBuilder class