justinas
(Justinas)
January 19, 2011, 10:26pm
1
Hi,
I am trying to join two table data using CActiveDataProvider.
Table Vehicles :
vehicle_id
…
owner_id
Table Owners :
owner_id
owner_name
…
$dataProvider=new CActiveDataProvider('Vehicles', array(
'criteria' => array(
"order" => "vehicle_id DESC",
'join'=>'LEFT JOIN owners ON owners.owner_id = t.owner_id',
'select'=>'*',
)
));
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?
justinas
(Justinas)
January 20, 2011, 8:00pm
2
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(
'criteria'=>array(
'with'=>array('owner'=>array('joinType'=>'LEFT JOIN')),
'order'=>'vehicle_id DESC',
)
));
justinas
(Justinas)
January 20, 2011, 10:16pm
4
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?
Justinas:
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?
justinas
(Justinas)
January 20, 2011, 10:23pm
6
It’s like you posted before:
$dataProvider=new CActiveDataProvider('Vehicles', array(
'criteria' => array(
'with'=>array('owners'=>array('joinType'=>'LEFT JOIN')),
"order" => "vehicle_id DESC",
)
));
Justinas:
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?
justinas
(Justinas)
January 20, 2011, 10:44pm
8
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(
'dataProvider'=>$dataProvider,
'itemView'=>'_vehicles_view',
)); ?>
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:
$dataProvider->owners->owner_name;
You cannot access the owner_name directly from your dataprovider.
justinas
(Justinas)
January 20, 2011, 11:25pm
10
To access the owner’s name you have to go through the ‘owners’ relation:
$dataProvider->owners->owner_name;
You cannot access the owner_name directly from your dataprovider.
Antonio, you are the best! Thank you.
cent040
(Arfan67)
May 13, 2014, 7:48am
11
you are the best Antonio and Justinas
Thank you