I want to show a other model field in my cgrid view , I am using yii model.
My current model is Members and other model is Billing:
My Code:
public function getImportantMembers(){
$criteria = new CDbCriteria;
$criteria->select ='t.*,b.billing_id,b.amount,b.billing_date,b.member_id,b.billing_status';
$criteria->join = 'JOIN billing AS b ON b.member_id = t.id ';
if(isset($_GET['condition']) AND $_GET['condition'] > 0){
$condition = $this->getConditionForImportantMembers($_GET['condition']);
$criteria->addCondition($condition);
}
else{
$criteria->addCondition("b.billing_date > DATE_SUB(NOW(),INTERVAL 2 MONTH) AND b.billing_status='c' AND b.amount >= 150 AND t.status='a'");
}
$criteria->group = 't.id';
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),
));
}
You should first clear the relation in your member model;
'billing' => array(self::BELONGS_TO, 'Billing', 'id'),
In your view:
$data->billing->amount
Related
I am trying to implement a dropdown filter in Cgridview, The column is from another table.
//model search code
//person table
public function search(){
$criteria=new CDbCriteria;
$criteria->alias='per';
$criteria->compare('LastName',$this->LastName,true);
$criteria->compare('FirstName',$this->FirstName,true);
$criteria->join='right JOIN person_surveys ps ON ps.id = per.P_Id';
}
//relation to person survey
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'PersonSurvey', 'P_Id'),
);
}
//person survey table model search
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('person_id',$this->person_id,true);
$criteria->compare('survey_ids',$this->survey_ids,true);
$criteria->join='left JOIN persons ps ON ps.id = per.P_Id';
}
//controller
public function actiongriddisplay(){
$model2=new Persons('search');
if(isset($_GET['ajax'])){
$model2->attributes =$_GET['Persons'];
$this->render('griddisplay',array('model2'=>$model2));
}
else{
$this->render('griddisplay',array('model2'=>$model2));
}
}
//in view file
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model2->Search(),
'filter' => $model2,
'ajaxType'=>'GET',
// 'ajaxUpdate'=>'items',
//'ajaxUrl' =>$this->createUrl('userManagement'),
'columns' => array(
array(
'name' => 'FirstName',
'header'=>'Full Name',
),
array(
'name' => 'user.survey_ids',
'header'=>'Survey ids',
'filter'=>CHtml::activeDropDownList($model2 ,'survey_ids', array('224'=>'224','223'=>'223','225'=>'225')),
'value'=>'isset($data->user->survey_ids)?$data->user->survey_ids:null'
),
I have put the search on, in models. why is the dropdown filter not working?
Why this is not working please help?
This link (http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/) will solve your problem.
I want to display email inbox with read and unread status and also display them in descending order. Here is my code:
$criteria = new CDbCriteria;
$criteria->order = 'emailid DESC';
$model = Email::model()->findAllByAttributes(
array(
'to_userid' => Yii::app()->user->id,
),
array(
'condition' => 'email_status=2 OR email_status=1',
), $criteria
);
$this->render('inbox', array(
'model' => $model,
));
But this is not working properly.
I don't think thats the right way of using cdbcriteria. Here, try this:
$criteria = new CDbCriteria();
$criteria->condition = 'to_userid=:userId AND (email_status=2 OR email_status=1)';
$criteria->params = array(':userId'=>Yii::app()->user->id);
$criteria->order = "emailid DESC";
$model = Email::model()->findAll($criteria);
$this->render('inbox', array(
'model' => $model,
));
This should work. Hope that helps. :)
i have order table where in product_id is string Like 10,11,12,13. And have Product table with this id.
how to get dataProvider with each product_id
My code is
public function getProducts($id){
$idarray = explode(',', $id);
$dataProviderProduct = Array();
foreach($idarray as $i=>$id){
$dataProviderProduct[$i]=new CActiveDataProvider('Product',
array( 'criteria'=>array(
'condition'=>'id=:id',
'params'=>array(':id' => $id),
),
'pagination'=>array( 'pageSize'=>10),
)
);
}
return $dataProviderProduct;
}
But this is wrong code
Use addInCondition like this:
$idarray = explode(',', $id);
$criteria = new CDbCriteria();
$criteria->addInCondition('id', $idarray);
$data = ModelName::model()->findAll($criteria);
i have two tables: User and User_works (User HAS_MANY User_works).
How can I add a condition to be displayed only users with certain works
User contains fields: id | name | Other information
User_works: id | user_id | work_id
User Model:
public function relations()
{
return array(
'works'=>array(self::HAS_MANY, 'UserWorks',
'',
'on' => 'works.user_id=t.id',
'together'=>false,
),
)
}
Controller:
$criteria = new CDbCriteria();
$criteria->with = array('works');
$criteria->compare = ????
Solution:
I wanted something like that:
SQL "Select user.id from User, User_works Where User_works.user_id=User.id AND User_works.work_id=$SOMEVALUE"
User Model:
public function relations()
{
return array(
'works'=>array(self::HAS_MANY, 'UserWorks',
'',
'joinType' => 'INNER JOIN',
'on' => 'works.user_id=t.id',
'together'=>true,
),
)
}
Controller:
$criteria = new CDbCriteria();
$criteria->with = array('works'=>array('on' => 'works.user_id=t.id AND (works.work_id=$SOMEVALUE OR ...)'));
As a result, I get the users with the necessary works.
But а new problem arose. The number of pages in the Listview not correctly displays.
List view does not consider the condition of necessary works. As a result, a number of page is wrong.
Solution :
$dataProvider=new CActiveDataProvider('User', array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>1,
),
));
$dataProvider->setTotalItemCount(count(User::Model()($criteria)));
or
Instead of setting the dataprovider criteria:
$dataProvider->criteria = $criteria
I set dataprovider->model criteria:
$dataProvider->model->setDbCriteria($criteria)
$criteria = new CDbCriteria();
$criteria->with = array('works');
$criteria->compare('works.theField_name' , $someThing);
I've a table with 2 mysql DATE field 'validFrom' and 'validTo'.
I need to allow user to search for a period beetween validFrom and validTo (included)
search using
validFrom >= searched validFrom
and
validTo <= searched validTo
What must I change ? the search() ? .
The Gii created code tell me this (i think it's a 'LIKE %string%' search )
$criteria->compare('validFromDate',$this->validFromDate,true);
$criteria->compare('validToDate',$this->validToDate,true);
$criteria->addBetweenCondition('colName', $this->validFrom, $this->validTo);
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->with = array('user');
$criteria->compare('nota_id', $this->nota_id, true);
$criteria->compare('user.nume', $this->filter_nume, true);
$criteria->compare('persoana1', $this->persoana1, true);
$criteria->compare('persoana2', $this->persoana2, true);
$d = $this->getFilterDateRange($this->data_discutie);
if (!empty($d[0]) && !empty($d[1])) {
// between condition
$criteria->addBetweenCondition('data_discutie', $d[0], $d[1]);
} else if (!empty($d[0])) {
$criteria->compare('data_discutie', '>=' . $d[0]);
} else if (!empty($d[1])) {
$criteria->compare('data_discutie', '<=' . $d[1]);
}
$d = null;
//echo $this->urmatoarea_discutie;
$d = $this->getFilterDateRange($this->urmatoarea_discutie);
//print_R($d);
if (!empty($d[0]) && !empty($d[1])) {
// between condition
$criteria->addBetweenCondition('urmatoarea_discutie', $d[0], $d[1]);
} else if (!empty($d[0])) {
$criteria->compare('urmatoarea_discutie', '>=' . $d[0]);
} else if (!empty($d[1])) {
$criteria->compare('urmatoarea_discutie', '<=' . $d[1]);
}
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => '15',
),
'sort' => array(
'defaultOrder' => 'coalesce(nullif(t.urmatoarea_discutie, ""), \'zzzzzz\') ASC, t.data_discutie ASC',
'attributes' => array(
'urmatoarea_discutie',
'data_discutie',
'persoana2',
'persoana1',
'filter_nume' => array(
'asc' => 'user.nume asc',
'desc' => 'user.nume desc'
),
),
),
));
}
protected function getFilterDateRange($key) {
$ret = array(&$from, &$to);
switch ($key) {
case '-1':
// past
$to = strtotime('next hour');
break;
case '1':
// future
$from = strtotime('last hour');
break;
case '-7':
$from = strtotime('-1 week last monday midnight');
$to = strtotime('last sunday midnight');
break;
case '7':
$from = strtotime('last monday');
$to = strtotime('next sunday midnight');
break;
}
return $ret;
}
Depending on what you want to do, the search() in the model is not necessarily be the place you want to be looking. Here is a standard Yii query:
$model = WhateverModel::model()->findAll(array(
"condition" => "'.date('Y-m-d H:i:s' BETWEEN validRrom AND validTo).'",
));
You will have to give a bit more detail if you more detailed help :)