CDbCriteria error in yii - yii

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. :)

Related

How to do a default sort on cgridview in yii1

i have the following cgrid view
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'complaint-job-grid',
'dataProvider'=>$model->search($complaint),
//~ 'filter'=>$model,
'columns'=>$columns,
'enableSorting'=>true,
));
my model code is
public function search($complaint)
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('complaint_job','user');
$criteria->compare('complaint_job.job_desc',$this->job_search, true);
if($this->user_search)
$criteria->addSearchCondition('CONCAT(first_name," ",last_name)', $this->user_search);
$criteria->compare('complaint_id',$complaint);
$criteria->compare('id',$this->id);
$criteria->compare('job_id',$this->job_id);
$criteria->compare('local_description',$this->local_description,true);
$criteria->compare('employee_id',$this->employee_id);
$criteria->order = 't.id ASC';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
so what i basically want is to show values in the cgridview in descending order of id(primary key). is it possible?
i tried like this with no success
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'id ASC',
),
You're doing sort in CDbCriteria;
That's why sorting in CGridView which uses CActiveDataProvider not work.
Remove from your model:
$criteria->order = 't.id ASC';
And add to CActiveDataProvider
'defaultOrder'=>'t.id ASC',

Yii cgridview cactivedataprovider doesnt work properly

I configured the Yii cactivedataprovider as the documentation writes:
$criteria = new CDbCriteria();
$criteria->together = true;
$criteria->with = array(
'relationId0',
'relationId1',
...
);
$criteria->compare('"relationId0".property0', $this->relationId0_property0, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => array(
'attributes' => array(
...
'relationId0.property0' => array(
'asc' => '"relationId0".property0',
'desc' => '"relationId0".property0 DESC',
),
...
)
)
));
so, when the ->together is false, then the gridview works properly and gets all rows what the pagination allowed, but in this case the compare (so the search) doesnt work (because this way doesnt use the related objects in the sql query),
but when ->together is true (and it is the solution supposedly) the compare is working but the gridview gets random number of rows in each page.
Thank you for helping.
Try to add pagination into your dataprovider.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination'=>array('pageSize'=>10),
'sort' => array(
'attributes' => array(
...
'relationId0.property0' => array(
'asc' => '"relationId0".property0',
'desc' => '"relationId0".property0 DESC',
),
...
)
)
));
This should solve your problem.

filter grideview work with having condition in search function in criteria?

i have a grideview by this code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'lecture-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header'=>'name',
'type' => 'raw',
// 'name'=>'name',
'value' => 'CHtml::link($data->name,Yii::app()->baseUrl . "/uploads/" . $data->name)',
),
array(
'header'=>'pages',
'value'=>'$data->slide_num'
),
array(
'header'=>'type',
'value'=>'$data->type'
),
array(
'header'=>'Size',
'value'=>'$data->size'
),
// 'subject.name'
array (
'header'=>'subject',
'value' => 'ucfirst($data->subject->name)',
'filter' => CHtml::dropDownList('Lecture[subject_id]',
$model->subject_id, Chtml::ListData(Subject::model()->findAll('department_id='.$department_id),'id','name'),
array('empty' => '(Select)'))
)
/* array(
'name'=>'subject',
'type'=>'raw',
'value'=>"Subject::model()->find('id=2')->name",
'filter'=>''
),*/
),
));
and in model the search function is
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('slide_num',$this->slide_num,true);
$criteria->compare('type',$this->type,true);
$criteria->compare('size',$this->size,true);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('subject_id',$this->subject_id);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
/*return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));*/
}
every thing is ok my target is filter that works in this search function correctly but i want to have a condition in criteria
$criteria->condition='subject_id=1 or subject_id=3';
when i put the condition in criteria its filter don't work any solution for having condition in search function and its filter work too
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('slide_num',$this->slide_num,true);
$criteria->compare('type',$this->type,true);
$criteria->compare('size',$this->size,true);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('subject_id',$this->subject_id);
$criteria->condition='subject_id=1 or subject_id=3';
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
/*return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));*/
}
Instead of $criteria->condition='subject_id=1 or subject_id=3';
try
$criteria->addCondition("subject_id = 1");
$criteria->addCondition("subject_id = 2", "OR");
OR JUST
$criteria->addInCondition("subject_id", array("1","2"));

How create condition to relations Yii

I have relations in Yii:
public function relations() {
return array(
'applicant' => array(
self::BELONGS_TO,
'ApplicantProfile',
array('applicant_id' => 'id'),
),
'filePair' => array(
self::BELONGS_TO,
'DocumentCategoryFile',
array('file_pair_id' => 'id'),
'joinType' => 'inner join'
)
);
}
How i can create addCondition in CDbCriteria() for aplicant, for example i want do is_deleted = 0. I am tried it $criteria->with = array('applicant' => array('join'=>'left join','condition'=>'aplicant.is_deleted = 0')); but i have sql syntax error
Thanks
Try this -
$criteria = new CDbCriteria();
$criteria->with = array('applicant');
$criteria->together = true;
$criteria->addCondition('applicant.is_deleted=0');
You should refer the link here to learn how to use it.

Yii ClistView pagination not working

My Clistview Pagination is not working properly.I tried every thing but still not getting success.Here is my model function:
public function getallone()
{
$criteria = new CDbCriteria;
$criteria->select = "t.id";
$criteria->condition = "t.featured=1 AND t.status = 1";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => array('pageSize' => 25),
));
}
Am still getting only 10 items per page but i want 25.I tried to 2 items but its also not working. Whats wrong with that?Didnt find.
And this is my view code:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->getallone(),
'itemView'=>'_allone',
'enablePagination' => true,
)); ?>
You shoud try this for better pagination option
$condition='type=:type';
$dataArray[':type']=$type;
$dataProvider=new CActiveDataProvider('modelname', array(
'criteria'=>array(
'select'=>'*',
'condition'=>$condition,
'params'=>$dataArray,
'order'=>'id DESC',
'offset'=>($page-1)*$limit,
'limit'=>$limit,
),
'pagination'=>array(
'pageSize'=>$limit,
),
));
$totalActivity= $dataProvider->getTotalItemCount();
http://www.yiiframework.com/doc/api/1.1/CActiveDataProvider for reference
try and use CArrayDataProvider
here is how do it:
public function actionIndex() {
$command = Yii::app()->db->createCommand("
SELECT u.id as u_id, u.link as u_link, u.title as u_title, u.description as u_description, u.public as u_public, u.created as u_created, u.status as u_status, w.id as w_id, w.domain as w_domain, w.status as w_status
from url as u
left join website as w
on w.id = u.website_id
where u.title!='null' and u.status = 1 and w.status = 1 and u.public = 1;
");
$news = $command->queryAll();
$dataProvider = new CArrayDataProvider($news, array(
'id' => 'user',
'keyField' => 'u_id',
'sort' => array(
'defaultOrder' => 'u_id desc',
),
'pagination' => array(
'pageSize' => 10,
),
));
$model_website = Website::model()->findAllByAttributes(array('status' => 1), array('limit' => 30, 'order' => 'id desc'));
$this->render('index', array(
'model_website' => $model_website,
'dataProvider' => $dataProvider,
));
}
and the clistview:
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'template' => "{summary}\n{pager}\n{items}\n{summary}\n{pager}",
'itemView' => '_index',
'pager' => array(
'maxButtonCount' => 10,
),
)
);