using cgridview show new entry at top yii - yii

i'm new in Yii I want to show new inserted entry at top in CGridView in Yii, but dont know how it is possible?
public function search()
{
$criteria=new CDbCriteria;
$criteria->alias = "jb";
$criteria->compare('title', $this->title, true);
$criteria->compare('notes', $this->notes, true);
$criteria->compare('createdon', $this->createdon, true);
$criteria->compare('expirydate', $this->expirydate, true);
$criteria->join= 'INNER JOIN company co ON (co.companyid=jb.companyid)';
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));

Why don't you use relations for your join?
$criteria=new CDbCriteria;
$criteria->alias = "jb";
$criteria->compare('title', $this->title, true);
$criteria->compare('notes', $this->notes, true);
$criteria->compare('createdon', $this->createdon, true);
$criteria->compare('expirydate', $this->expirydate, true);
$criteria->with('company');
$criteria->together=>true;
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
and then defaultScope for jb model
public function defaultScope()
{
$alias = $this->getTableAlias(false,false);
return array(
'order'=>"`$alias`.`id` DESC",
);
}

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',

CDbCriteria error in 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. :)

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"));

Yii:Property "CActiveDataProvider.select" is not defined

I am using a slave db, and when i switch the db connection, this is what i get:
what does this mean?
CException
Property "CActiveDataProvider.select" is not defined.
public function actionIndex() {
$criteria = new CDbCriteria();
$criteria->condition = 'user_id = ' . Yii::app()->user->id;
VillageSlaveM::$server_id = Yii::app()->session['user_active_world'];
$dataProvider = new CActiveDataProvider('VillageSlaveM', $criteria);
$this->render('index', array(
'dataProvider' => $dataProvider,
));
}
Instead of line:
$dataProvider = new CActiveDataProvider('VillageSlaveM', $criteria);
try
$dataProvider = new CActiveDataProvider('VillageSlaveM', array('criteria'=>$criteria));
I'm not sure, but documentation describes CActiveDataProvider's construct.

yii update clistview by dropdownlist

I'm a very newbie in Yii framework. I would like to create a page. Which is when the dropdown list change, the listview/gridview will be change by dropdown value.
this is my view
<div class="row">
<?php
$records = Company::model()->findAll();
$company_list = CHtml::listData($records, 'id', 'name');
echo CHtml::dropDownList('company_id','', $company_list,
array(
'onchange'=>"$.fn.yiiListView.update('ajaxListView', {url: '".Yii::app()->createUrl('department/dynamicsectionlist')."?company_id='+$('#company_id option:selected').val()})",
'prompt'=>'Please select a company',
)); ?>
</div>
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view_section',
'id'=>'ajaxListView',
));
?>
This is model
public function search()
{
// Warning: 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('p_id',$this->p_id);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
$criteria->compare('company_id',$this->company_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is Controller
public function actionDynamicsectionlist()
{
$company_id = $_POST['company_id'];
$criteria=new CDbCriteria();
$criteria->condition .= 't.id IN (SELECT t2.id, t2.name FROM department t2 WHERE t2.company_id = :company_id)';
$criteria->params[':company_id'] = $company_id;
$dataProvider = new CActiveDataProvider( 'Department', array( 'criteria' => $criteria, ) );
$this->render( 'sectionlist', array( 'dataProvider' => $dataProvider ) );
}
But it is not working. Please help me.
Regads
Tharsoe
I solved it.
This is controller
// Initial view (department/depatmentlist)
public function actionDepartmentlist()
{
$model=new Department('search');
$model->unsetAttributes(); // clear any default values
$model->p_id = 0;
// $dataProvider->getData() will return a list of Post objects
// $dataProvider=new CActiveDataProvider('Department');
$this->render('list_department',array(
'model'=>$model,
));
}
// when the user selected the company from dropdown list
public function actionDynamicsectionlist()
{
$model=new Department('dsearch');
$model->unsetAttributes(); // clear any default values
$model->p_id = 0;
if(isset($_GET['company_id']))
$model->company_id = $_GET['company_id'];
$this->render('sectionlist',array(
'model'=>$model,
));
}
This is model (nothing change)
public function search()
{
// Warning: 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('p_id',$this->p_id);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
$criteria->compare('company_id',$this->company_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is view (list_department.php)
<h1>Departments List</h1>
<div class="row">
Company<br />
<?php
$records = Company::model()->findAll();
$company_list = CHtml::listData($records, 'id', 'name');
echo CHtml::dropDownList('company_id','', $company_list,
array('prompt'=>'Please select a company',)); ?>
</div>
<?php
/*
for ListView
$this->widget('zii.widgets.CListView', array(
//'dataProvider'=>$dataProvider,
'dataProvider'=>$model->search(),
'itemView'=>'_view_section',
'id'=>'ajaxListView',
));
*/
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'department-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
//'p_id',
'created',
'updated',
//'company_id',
),
));
?>
<?php
Yii::app()->clientScript->registerScript('search',
"$('#company_id').change(function(){
var companyId = $('#company_id option:selected').val();
$.fn.yiiGridView.update(
'department-grid',
{ type: 'GET',
url: 'http://localhost/mmaig_ceo/ceo-control-system/index.php?r=department/dynamicdepartmentlist&ajax=department-grid&company_id=' + companyId
}
);
});
")
?>