I'm using Yii with Firebird. Let's assume we have following code:
public function actionGetCustomer($limit = -1, $start = 0) {
$arr = Yii::app()->db->createCommand()
->select('*')
->from('customer')
->leftJoin('contact', 'customer.idcustomer = contact.idcontact')
->limit($limit, $start)
->queryAll();
$ans['arr'] => $arr;
return $ans;
}
Then we want to take count of records in db and have following:
public function actionGetCustomer($limit = -1, $start = 0) {
$arr = Yii::app()->db->createCommand()
->select('*')
->from('customer')
->leftJoin('contact', 'customer.idcustomer = contact.idcontact')
->limit($limit, $start)
->queryAll();
//copy & paste & edit
$cnt = Yii::app()->db->createCommand()
->select('count(*)')
->from('customer')
->leftJoin('contact', 'customer.idcustomer = contact.idcontact')
->queryScalar();
$ans['arr'] => $arr;
$ans['cnt'] => $cnt;
return $ans;
}
And to remove doubling of code (actual query have 10 joins and tons of fields) is it possible to achieve something like this?
public function actionGetCustomer($limit = -1, $start = 0) {
$cmd = Yii::app()->db->createCommand()
->from('customer')
->leftJoin('contact', 'customer.idcustomer = contact.idcontact');
$arr = $cmd
->select('*')
->limit($limit, $start)
->queryAll();
//copy & paste & edit
$cnt = $cmd
->select('count(*)')
->queryScalar();
$ans['arr'] => $arr;
$ans['cnt'] => $cnt;
return $ans;
}
Tried this, but answer from db was cached(?) and despite of changing select part, query scalar was returning result of previous query. Also tried clone and manually copying parts of old command into new but there were errors. As for now the only alternative I see is plain SQL.
Note that first query have limit, whilst second don't.