CListView filtering by value - yii

is there a parameter that I can use with ClistView to get the commments of a given post_id
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comment',
'template'=>"{items}\n{pager}",
)); ?>
I'd like to add post_id = value in order to only list the comment with the given post_id
I put a search function in my comment model
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('post_id',$this->post_id,true);
return new CActiveDataProvider('Comment', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'status, create_time DESC',
),
));
}
Thank in advance for your help. I cannot find in the documentation.

Related

Yii searching duplicated results

I'm new to Yii and this is not my code
When i search something, the results are duplicated, Like
Results:
product1
product1
product2
product2
And I'm sure that the problem is coming from the Controller because i checked the view.
The Controller:
public function actionIndex($q){
$criteria=new CDbCriteria;
$criteria->with=array('cat','postDesc');
$criteria->together=true;
// $criteria->compare('post.pack_type',2);
$criteria->compare('cat.zone','Group',true);
$criteria->compare('postDesc.name',$q,true);
// $criteria->compare('category_id',$id);
$this->pageTitle = "The search resluts for:".$q;
Yii::app()->clientScript->registerMetaTag(Helpers::config('meta_keyword'), 'keywords');
Yii::app()->clientScript->registerMetaTag(Helpers::config('meta_description'), 'description');
$provider=new CActiveDataProvider('PostCategory', array(
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),
'criteria'=>$criteria,
'sort' => array(
'defaultOrder' => 't.post_id desc',
),
));
$this->render('index',array(
'dataProvider'=>$provider,
'cat'=>$cat
));
}
public function actionItem($id){
$model=Post::model()->findByPK((int)$id);
$this->pageTitle = $model->desc->name;
Yii::app()->clientScript->registerMetaTag(Helpers::config('meta_keyword'), 'keywords');
Yii::app()->clientScript->registerMetaTag(Helpers::config('meta_description'), 'description');
$images=PostImage::model()->findAll(array('condition'=>'post_id='.$id));
$attrs=PostAttr::model()->findAll(array('condition'=>'post_id='.$id,'order'=>'post_attr_id'));
$media=PostMedia::model()->findAll(array('condition'=>'post_id='.$id));
$this->render('item',array(
//'dataProvider'=>$provider,
'model'=>$model,
'images'=>$images,
'attrs'=>$attrs,
'media'=>$media,
));
}
So I want it to send not two, but one result of every product.
It may happen if your model contains multiple postDesc or cat related records. Solution may differ depending on what result you want to achieve in this situation, but the simplest one is probably group by PostCategory private key field:
$criteria->group = 't.id';

How to get names in alphabetic order in Yii

In my Yii web application, having student table for saving student details. I want to student name in alphabetical order when retrieve data from student table.
public function defaultScope() {
return array("order" => "student_firstname");
}
I tried this function, but not working properly.
Please help me.
Thanks in advance.
you can use the defaultOrder property of CSort. For example...
$dataProvider=new CActiveDataProvider('Example', array(
'sort'=>array(
'defaultOrder'=>array(
'student_firstname'=>false
)
)
));
In model's search method for data provider try to set default order, like this:
$dataProvider=new CActiveDataProvider($this, array(
'sort'=>array(
'defaultOrder'=> 'student_firstname ASC'
)
));
In model's search method for data provider try to set default order, like this:
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>Yii::app()->params['defaultPageSize'],
),
'sort'=>array(
'defaultOrder'=>array(
'student_firstname'=>CSort::SORT_ASC
),
),
));

cgridveiw to display fetched data via customized query

I am a newbei in yii, when a person clicks on a category display him all products under that particular category in a gridview
view
productcategory
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'admin-grid',
'dataProvider'=>$model->SelectedCategoryProducts,
'filter'=>$model,
'columns'=>array(
'Name',
'Model',
'Brand',
'Price',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
controller
product
public function actionProductcategory($id)
{
$model= Product::model()->SelectedCategoryProducts($id);
var_dump($model);
$this->render('productcategory',array(
'model'=>$model,'id'=>$id,
));
}
model
product
public function SelectedCategoryProducts($id)
{
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
var_dump($dataProvider);
return $dataProvider;
}
CException
Property "CActiveDataProvider.sellerSelectedCategoryProducts" is not defined.
PLEASE HELP! I am losing my mind on this ... not able to display in gridview.
Pass $id to your view file.
$this->render('productcategory',array('model'=>$model,'id'=>$id));
Then pass id to model function in ccgridview function.
'dataProvider'=>$model->SelectedCategoryProducts($id),
Hope this might help
Controller file
public function actionProductcategory($id)
{
$model=new Product;
$this->render('productcategory',array('model'=>$model,
'id'=>$id));
}
In View file
'dataProvider'=>$model->SelectedCategoryProducts($id),
UPDATE 1
'columns'=>array(
'name',
'model',
'brand',
'price',
change them to lowercase which are your original column names
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
this can retrieve needed data.... first check that data is perfect ... is it right after that take second step....

Date Field Search not filtering for 'dd/mm/yyyy' format - YII Framework

CJUIDatePicker not filtering value for dd/mm/yyyy format but filtering if i gave DB date format manually like shown in image, I have attached all codes including View and Model for Search panel and Date Widget, Please look into this,
My Model Search(),
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('crm_base_contact_id',$this->crm_base_contact_id);
$criteria->compare('name',$this->name,true);
$Date = date('Y-m-d',strtotime($this->created)); // get proper Y-m-d
$startOfDay = $Date . ' 00:00:00'; // from start of the day
$endOfDay = $Date . ' 23:59:59'; // until end of the day
// the rows between these
$criteria->addBetweenCondition('created', strtotime($startOfDay) , strtotime($endOfDay) );
$criteria->compare('createdby',$this->createdby,true);
$criteria->compare('description',$this->description);
$criteria->compare('is_active',$this->is_active);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
My View for CGridView,
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'basecontact-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
array('class'=>'CButtonColumn',),
'name',
'crm_base_contact_id',
array(
'name'=>'is_active',
'filter'=>CHtml::dropDownList('Event[is_active]', '',
array(
''=>'',
'1'=>'Y',
'0'=>'N',
)
),
'value' =>'($data->is_active==1)?"Y":"N"',
),
'created',
'createdby',
'description',
),
)); ?>
My View Code for Search column,
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'model'=>$model,
'id'=>'Search-Created',
'attribute'=>'created',
'options'=>array(
'dateFormat'=>'dd/mm/yy',
'showAnim'=>'fold',
'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
'buttonImageOnly'=>true,
'buttonText'=>'',
'showAnim'=>'fold',
'showOn'=>'button',
'showButtonPanel'=>false,
),
));?>
you should probably change the date format. set it like this, 'dateFormat'=>'dd-mm-yy',
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'dob',
// additional javascript options for the date picker plugin
'options' => array(
'showAnim' => 'fold',
'dateFormat'=>'dd/mm/yyyy',
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));
Update:
In your model, define it as following, in my case dob is the attribute name. you can have your own instead.
This will change your dateformat from "dd/mm/yyyy" to "yyyy-mm-dd internally before you run any find.
Ref : http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeFind-detail
protected function beforeFind()
{
$this->dob=date('Y-m-d', strtotime($this->dob));
parent::beforeFind();
}
update 2
Replace your search method with this,
public function search() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('crm_base_contact_id', $this->crm_base_contact_id);
$criteria->compare('name', $this->name, true);
$criteria->compare('created', '07/12/2013'); //date('Y-m-d', strtotime($this->created))
$criteria->compare('createdby', $this->createdby, true);
$criteria->compare('description', $this->description);
$criteria->compare('is_active', $this->is_active);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Update 3
change this line $criteria->compare('created', '07/12/2013'); to
$criteria->compare('created', date('Y-m-d', strtotime($this->created));

Custom search in Yii model for CGrid view on relational data

I have a custom search in my model noted below, It works with out any issue in MySQL and when I step through the code with NetBeans Yii builds the query properly but returns no rows. I run the same query in MySQL and I get the intended rows of information. Is there something I am doing wrong?
public function fnSearch(){
$nParts=explode(" ", $_REQUEST['fnText']);
$criteria=new CDbCriteria;
$criteria->condition='first_name like ":fname" and last_name like ":lname"';
$criteria->params=array(';fname'=>trim($nParts[0]).'%', ':lname'=>trim($nParts[1]).'%');
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
My test form looks like this:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'test-form',
'enableAjaxValidation'=>false,
)); ?>
<?= CHtml::textField('fnText'); ?>
<?php $this->endWidget(); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employees-grid',
'dataProvider'=>$model->fnSearch(),
// 'filter'=>$model,
'columns'=>array(
'first_name',
'last_name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
The controller has this:
public function actionTest(){
$model=new Employees;
$this->render('test',array(
'model'=>$model,
));
}