isla
(Swisscom90)
May 16, 2012, 10:34am
1
I have a CGridview that lists employees, when you click on a employee you will be redirected to the view of that id. In this view I show information about the employee. Below the information I want to display a CGridview with information about all the projects this particular employee has worked with.
The employee-table has not a column for "projects", but the project-table has an employee_id.
I guess the easiest way would be to use the GET[id] from the url and just call the project-model and match it with the employee_id.
Im new to yii so im wondering which of the find() methods I should use. I want to use GET[] and match it with a column in the projectmodel. Is this possible? Maybe there is a better way to handle this?
yiwi
(Kdietrich9000)
May 16, 2012, 10:39am
2
Hey,
just define a relation between your two models in Employee model:
'projects' => array(self::HAS_MANY, 'Project', 'employee_id'),
You can fetch than the data with:
$model = Employee::model()->with('projects')->findByPk($your_employee_id);
$id = CPropertyValue::ensureInteger($_GET['id']);
$employee = Employee::model()->findByPk($id);
then
$projects = Project::model()->findAllByAttributes(array('employee_id'=>$id));
or just
$projects = $employee->projects
if you use relations
hope this helps.
yiwi
(Kdietrich9000)
May 16, 2012, 11:22am
4
I highly recommend using model relations from beginning to get your data with only one query.
isla
(Swisscom90)
May 16, 2012, 2:46pm
5
I tried with yiwis method.
I can’t get it to display the rows in the CGridview.
I guess I need to do something instead of $model=new Employee(‘search’); ?
This is my controller:
public function actionView($id)
{
$model = Employee::model()->with('projects')->findByPk($id);
$this->render('view',array(
'model'=>$model,
));
}
and this is in the view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employee-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'projects.name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
yiwi
(Kdietrich9000)
May 16, 2012, 9:03pm
6
Hey,
you should definitely take a look at the yii guide and wiki.
This should do the trick:
$dataProvider = new CActiveDataProvider(Projects::model(), array(
'criteria'=>new CDbCriteria(array(
'condition' => 'employee_id=?',
'params' => array($id),
)),
));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employee-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'another_attribute',
array(
'class'=>'CButtonColumn',
),
),
));
isla
(Swisscom90)
May 17, 2012, 11:40am
7
Will do! Think it’s a bit messy tho
It works now, thanks!
One question tho, in this code:
'condition' => 'employee_id=$id',
I tried to insert an $id variable but I only get errors, I guess that the variable is handled as a string, how can i escape this?
yiwi
(Kdietrich9000)
May 17, 2012, 11:43am
8
isla:
Will do! Think it’s a bit messy tho
It works now, thanks!
One question tho, in this code:
'condition' => 'employee_id=$id',
I tried to insert an $id variable but I only get errors, I guess that the variable is handled as a string, how can i escape this?
array(
'condition'=>'employee_id=:employee_id',
'params'=>array(':employee_id'=>$id)
)
This ensures that no malicious content can be passed, because yii automatically encodes the variables.
isla
(Swisscom90)
May 17, 2012, 12:27pm
9
Thanks!
Is there an easy way to implement the advance-search in this CGridview?
yiwi
(Kdietrich9000)
May 17, 2012, 2:57pm
10
Just take a look at this tutorial:
As you can see, you must add the $filter property to create a search behaviour.