I'm getting an error when i add in STAT. I'm trying to count the number of responses for each user and sort it from desc order.
My error: CActiveDataProvider and its behaviors do not have a method or closure named "getValidators".
relation:
'stickerCount'=>array(self::STAT, 'UserSticker', 'user_id'),
controller:
$likes=new CActiveDataProvider('UserSticker');
$this->render('index',array(
'likes'=>$likes,
'sort'=>array(
'defaultOrder'=>'stickerCount DESC',
),
));
<?php
$this->widget ( 'bootstrap.widgets.TbGridView', array (
'id'=>'follower',
'type'=>'striped condensed',
'dataProvider'=>$likes,
'filter'=>$likes,
'columns'=> array(
'stickerCount',
)));
?>
try this:
$criteria = new CDbCriteria();
$criteria->join = 'JOIN (SELECT user_id, count(*) As stickerCounter '
. 'FROM user_sticker GROUP BY user_id) userSticker '
. 'ON userSticker.user_id = t.id';
$likes = new CActiveDataProvider('MainTable',array(
'criteria'=>$criteria,
));
...
Related
I need to conver this sql statement that has a multi column group by clause into CActiveDataProvider in Yii version 1.1.4?
select * from my_table_name group by bookname,categorytitle,bookkey order by bookname,categorytitle,bookkey asc;
I need a CActiveDataProvider to feed a CGridView search function. Apparently, I only have a simple return in my model of the search function that runs my CGridView
return new CActiveDataProvider(get_class($this),
array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'id ASC'
),
'pagination' => array('pageSize' => ActiveRecord::PAGE_SIZE),
));
so how to convert the sql statement that I gave into CActiveDataProvider format ?
You can do using two way -
Make sql view and than create yii model and use this in your
controller or page view.
Create custom search function in your existing model and use group by
e.g. -
<?php
public function new_search(){
$criteria = new CDbCriteria();
...
..
$criteria->group = "bookname, categorytitle, bookkey";
$criteria->order = "bookname, categorytitle, bookkey";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>100)
));
}
?>
I have this very simple table in my view and I'm trying to display value from joined table:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'skills-grid',
'dataProvider'=>$model->searchWithStudentSuccessRate($id),
'template' => '{items}{pager}',
'cssFile'=>Yii::app()->request->baseUrl. '/themes/'. Yii::app()->theme->name.'/css/table.css',
'htmlOptions'=>array('class'=>'datagrid', 'style'=>'width:550px;'),
'columns'=>array(
array(
'name'=>'name',
),
array(
'name' => 'value',
'header' => Yii::t('MainTrans', 'Value'),
'value' => '$data->student_skills->value',
),
array(
'name' => 'successRate',
'header' => Yii::t('MainTrans', 'Success Rate'),
'value' => '$data->successRate."%"',
),
),
));
And this is the search function:
public function searchWithStudentSuccessRate($id)
{
// 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('t.threshold',$this->threshold);
$criteria->with = array('student_skills');
$criteria->together = true;
$criteria->select = array('IFNULL(CASE WHEN ROUND((student_skills.value/t.threshold)*100,0) > 100 THEN 100 ELSE ROUND((student_skills.value/t.threshold)*100,0) END,0) as successRate','*');
$criteria->group = "t.name";
$criteria->condition = 'student_skills.student_id = '.$id;
$criteria->compare('successRate',$this->successRate);
$criteria->compare('student_skills.value',$this->value);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>25,
),
'sort'=>array(
'defaultOrder'=>array(
'successRate'=>CSort::SORT_DESC,
),
'attributes'=>array(
'successRate'=>array(
'asc'=>'successRate',
'desc'=>'successRate DESC',
),
'*',
),
),
));
}
But I get error: "Trying to get property of non-object" when I added value column to my CGridView.
Everything works fine without column value (columns successRate and name are fine). Relation should be fine as well. The error I get means that the value doesn't exist but it should since I did something similar in my other views and there it works.
Can anyone tell what's wrong? I'm sure it's something minor but I'm struggling with this embarrasing problem for a while.
Thanks
It means that in some conditions $data->student_skills is NULL. Try change this:
'value' => '$data->student_skills->value',
to this
'value' => 'empty($data->student_skills) ? null : $data->student_skills->value',
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.
I have three tables
1. tbl_employee: id(PK), name, position_id(FK), type_id(FK)
2. tbl_position: id, position
3. tbl_type : id, type
I want to display records in field position like what sql does below.
SELECT tbl_employee.name, tbl_position.position
FROM tbl_employee, tbl_position
WHERE tbl_employee.position_id = tbl_position.id AND tbl_position.position LIKE '%Designer';
In my EmployeeController
public function actionIndex(){
$dataProvider=new CActiveDataProvider('Employee', array(
'criteria'=>array(
'select'=>'t.name, tbl_position',
'with'=>array(
'position'=>array('select'=>'position'),
'type'=>array('select'=>'type'),
),
'condition'=>"tbl_position.position LIKE '%Designer'",
),
));
$this->render('index',array('dataProvider'=>$dataProvider,));
}
and in my models
public function relations(){
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'position' => array(self::BELONGS_TO, 'Position', 'position_id'),
'type' => array(self::BELONGS_TO, 'Type', 'type_id'),
);
}
and this is my view
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
array(
'name'=>'position_id',
'value'=>CHtml::encode($model->position->position),
),
array(
'name'=>'type_id',
'value'=>CHtml::encode($model->type->type),
)
),
)); ?>
The error I've got is:
Active record "Employee" is trying to select an invalid column "tbl_position".
Note, the column must exist in the table or be an expression with alias.
How can I access position field in tbl_position by using join?What is the correct syntax for achieving that purpose.
Many thanks
'select'=>'t.name, position.position',
'condition'=>"position.position LIKE '%Designer'",
or
$model = TblEmployee::model()->with('position','type')->findall(array("condition"=>"position.position LIKE '%Designer'"));
Here I find the solution, but actually your answer was very close
public function actionIndex(){
$dataProvider=new CActiveDataProvider('Employee', array(
'criteria'=>array(
'select'=>'t.name',
'with'=>array(
'position'=>array('select'=>'position'),
'type'=>array('select'=>'type'),
),
'condition'=>"position.position LIKE '%Designer'",
),
));
$this->render('index',array('dataProvider'=>$dataProvider,));
}
Controller,
$model=Product::model()->display_products_statistics();
$this->render('admin',array(
'model'=>$model,
));
View,
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'product-grid',
'dataProvider' => $model->display_products_statistics(),
'filter'=> $model,
'columns'=> array(
'member_count',
'seller_count',
'visitor_count',
'lowest price',
'desc',
'price',
'createdate',
'updatedate',
'opid',
'pimg',
array(
'class'=>'CButtonColumn',
),
),
));
Model,
$sql="select member_count,seller_count,
visitor_count from fc_product fp
group by member_count,visitor_count,seller_count
order by member_count desc";
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$products_statistics=$command->queryAll();
$dataprovider=new CArrayDataProvider($products_statistics,
array('pagination'=>false));
return $dataprovider->getData();
But then I am getting this error
Fatal error: Call to a member function getData() on a non-object in D:\wamp\www\yii\framework\zii\widgets\CBaseListView.php on line 111
I am confused and don't know what's going wrong.
Your problem is that you are not using CDataProvider in the correct way. CGridView needs an instance of CDataProvider to display, but you have used CDataProvider->getData() instead. I'd also recommend using CSqlDataProvider, which is purpose made for custom sql queries. Try this in your model;
$count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM fc_product')->queryScalar();
$sql="select member_count,seller_count,
visitor_count from fc_product fp
group by member_count,visitor_count,seller_count
order by member_count desc";
return new CSqlDataProvider($sql, array(
'totalItemCount' => $count,
'sort' => array(
'attributes' => array(
'member_count', 'visitor_count', 'seller_count',
),
),
'pagination' => false