CActiveDataProvider and JOIN


I am trying to join two table data using CActiveDataProvider.

Table Vehicles:



Table Owners:



		$dataProvider=new CActiveDataProvider('Vehicles', array(

                    'criteria' => array(

                        "order" => "vehicle_id DESC",

                        'join'=>'LEFT JOIN owners ON owners.owner_id = t.owner_id',




Script does not display any errors, but I can’t get owner data in view (handling data with CListView widget). Maybe something missing in Vehicles model?

Nobody wants to help noob or my question is not clear? This is important for me…

Hi Justinas,

Have you created the relationships with the models?

// on Vehicles

public function relations(){

 return array('owner'=>array(self::BELONGS_TO,'Onwer','onwer_id'));


After you can create your CActiveDataProvider like this:

new CActiveDataProvider('Vehicles',array(


                'with'=>array('owner'=>array('joinType'=>'LEFT JOIN')),

                'order'=>'vehicle_id DESC',



Hi Antonio,

now relations is in place, but result from CActiveDataProvider still does not contain owner information (or I don’t know how to access it, trying same as vehicle data in view $data -> …).

Maybe I’ve missed something else?

Can you post the declaration of CActiveDataProvider here?

It’s like you posted before:

		$dataProvider=new CActiveDataProvider('Vehicles', array(

                    'criteria' => array(

                        'with'=>array('owners'=>array('joinType'=>'LEFT JOIN')),

                        "order" => "vehicle_id DESC",



Can I see also the relations on Vehicles please?

Relations on Vehicles model:

        public function relations()


            return array('owners'=>array(self::BELONGS_TO, 'Owners', 'owner_id'));


Then in view:

<?php $this->widget('zii.widgets.CListView', array(



)); ?>

And in _vehicles_view when I try to access $data -> owner_name I get error

[color=#800000][font=Verdana]Property "Vehicles.owner_name" is not defined.[/font][/color]

To access the owner’s name you have to go through the ‘owners’ relation:


You cannot access the owner_name directly from your dataprovider.

Antonio, you are the best! Thank you.

you are the best Antonio and Justinas

Thank you