Expand|Select|Wrap|Line Numbers
- if($ut=='Admin'){ // if the user is Admin the query should not have a filter
- $queryCustomer = Yii::app()->db->createCommand()
- ->select('cust.mobile_number as customer,sum(trans.price) as sales,trans.datetime')
- ->from('customers cust')
- ->join('transactions trans', 'cust.id=trans.customer_id')
- ->where('transaction_type_id=:id', array(':id'=>1))
- ->group('customer_id')
- ->order('trans.datetime DESC')
- ->queryAll();
- $per_customer=$this->renderTableCustomer($queryCustomer);
- } else { // if the user is not Admin the query should have a filter
- $queryCustomer = Yii::app()->db->createCommand()
- ->select('cust.mobile_number as customer,sum(trans.price) as sales,trans.datetime')
- ->from('customers cust')
- ->join('transactions trans', 'cust.id=trans.customer_id')
- ->where('transaction_type_id=:id', array(':id'=>1))
- ->andWhere('user_id=:userID',array(':userID'=>$filter))
- ->group('customer_id')
- ->order('trans.datetime DESC')
- ->queryAll();
- $per_customer=$this->renderTableCustomer($queryCustomer);
- }
For example. I put the query to function.
Expand|Select|Wrap|Line Numbers
- public function actionSample($queryCustomer){
- $queryCustomer = Yii::app()->db->createCommand()
- ->select('cust.mobile_number as customer,sum(trans.price) as sales,trans.datetime')
- ->from('customers cust')
- ->join('transactions trans', 'cust.id=trans.customer_id')
- ->where('transaction_type_id=:id', array(':id'=>1))
- ->group('customer_id')
- ->order('trans.datetime DESC')
- ->queryAll();
- return $queryCustomer;
- }
Expand|Select|Wrap|Line Numbers
- ->andWhere('user_id=:userID',array(':userID'=>$filter))
Expand|Select|Wrap|Line Numbers
- if($ut=='Admin'){ // if the user is Admin the query should not have a filter
- $results=$this->actionSample($queryCustomer);
- $per_day="$this->renderTable($results);
- else{
- $addWhere="->andWhere('user_id=:userID',array(':userID'=>$filter))";
- $results=$this->actionSample($queryCustomer,$addWhere);
- $per_day="$this->renderTable($results);
- }