tbl_user
========
id
name
etc
tbl_jobs
========
id
job_details
tbl_job_applications
====================
id
user_id
job_id
applied_date
I am trying to display a CGridView that returns all jobs that the current user has applied to. I need data from both the ‘jobs’ table and ‘job_applications’ table. I get the user_id using Yii::app()->user->id
I can’t get my head around this - it should be very simple to do but I’m struggling. There are 4 parts to this:
In my site I have a table Stories, which has a column called ‘eid’, a reference to a table called Engines. I needed to display the engine name associated with each story in the CGridView.
//actionIndex (or whatever action your CGridView is for)
$model=new Stories('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Stories']))
$model->attributes=$_GET['Stories'];
$this->render('index',array(
'model'=>$model,
));
Stories Index View (or whatever view you are using)
$dataProvider = new CActiveDataProvider("Stories", array(
'criteria'=>$criteria
));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'stories-grid',
'dataProvider'=>$dataProvider,
'filter'=>Stories::model(),
'columns'=>array(
array(
'name' => 'eid',
'header' => 'Engine',
'value' => '($data->engine->name)',
'filter' => $this->getEngineFilter(), //custom filter for engines, doesn't work yet...not important in displaying the engines
),
array(
'name' => 'date',
'value' => '($data->date)',
),
array(
'name' => 'summary',
'value' => 'substr($data->summary,0,100)'
),
$cbutton, //custom buttons, not really important
)
));
So that seemed to work for me. Unfortunately my filters are not working properly, but I am still working on that. At the very least, this displays all the appropriate information. Hope this helps.
new CActiveDataProvider($name, array(
'criteria'=>$criteria
));
the $criteria variable above, I am using my own criteria that I created, which is probably why the filter isn’t working. I’m not sure what you would put there, sorry.