Can someone explain how to dynamically build a query with query builder?
I am trying it like this but it only returns the object, no results. If I copy the text of the SQL in the object into mysql workbench then I get the results, so I know the query is good, it just seems that the way I am trying to use this isn’t right.
$pendingCount = Yii::app()->db->createCommand();
$pendingCount->select("COUNT(*)");
$pendingCount->from("invoice_items_$tran_id i");
$pendingCount->join('customers c', 'c.cust_id = i.cust_id');
$pendingCount->join('service_location s', 's.serv_id = i.serv_id');
$pendingCount->join('states ss', 'ss.id = s.state');
$pendingCount->where('i.invoice_id IS NULL AND i.is_voided=0');
if(isset($_GET['sSearch']) && $_GET['sSearch'] != ""){
for($i=0; $i<count($sColumns); $i++){
$pendingCount->andWhere($sColumns[$i]." LIKE '%:param_$i%'", array(":param_$i" => $_GET['sSearch']));
}
}
for($i=0; $i<count($sColumns); $i++){
if(isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != ''){
$pendingCount->andWhere($sColumns[$i]." LIKE '%:param2_$i%'", array(":param2_$i" => $_GET['sSearch']));
}
}
if(isset($_GET['iSortCol_0'])){
$sOrder = "";
for($i=0; $i<intval($_GET['iSortingCols']); $i++){
if($_GET['bSortable_'.intval($_GET['iSortCol_'.$i])] == "true"){
$sOrder .= $sColumns[intval($_GET['iSortCol_'.$i])]." ".($_GET['sSortDir_'.$i]==='asc'? "ASC" : "DESC").", ";
}
}
$sOrder = substr_replace($sOrder, "",-2);
if($sOrder != ""){
$pendingCount->order($sOrder);
}
}
if(isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1'){
$pendingCount->limit(intval($_GET['iDisplayLength']), intval($_GET['iDisplayStart']));
}
$pendingCount->queryAll();
text result is in the object, but results are not.
SELECT `i`.*, `c`.`cust_name`, `s`.`location_name`, `s`.`street`, `s`.`suite`, `s`.`city`, `s`.`zip`, `ss`.`stateCode`, `c`.`active` AS `cust_active`, `s`.`active` AS `serv_active`
FROM `invoice_items_9` `i`
JOIN `customers` `c` ON c.cust_id = i.cust_id
JOIN `service_location` `s` ON s.serv_id = i.serv_id
JOIN `states` `ss` ON ss.id = s.state
WHERE i.invoice_id IS NULL AND i.is_voided=0
ORDER BY `c`.`cust_name` ASC LIMIT 100