Hi!
I am working with Yii for quite some time now, and hit a problem i couldn’t solve/research today.
I got following structure:
User<—Rabatt—>Warengruppe—>Kabel
Rabatt relations
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'warengruppe' => array(self::BELONGS_TO, 'Warengruppe', 'warengruppe_id'),
);
}
Warengruppe relations (the needed ones)
public function relations()
{
return array(
...
'kabels' => array(self::HAS_MANY, 'Kabel', 'warengruppe_id'),
'rabatts' => array(self::HAS_MANY, 'Rabatt', 'warengruppe_id'),
);
}
Kabel relations
public function relations()
{
return array(
'warengruppe' => array(self::BELONGS_TO, 'Warengruppe', 'warengruppe_id'),
'kabelbefestigungs' => array(self::HAS_MANY, 'Kabelbefestigung', 'kabel_id'),
);
}
User relations
public function relations()
{
return array(
'rabatts' => array(self::HAS_MANY, 'Rabatt', 'user_id'),
);
}
What i want to do is display some information of all the tables inside a Gridview.
So first i generate the DataProvider as follows:
$criteria= new CDbCriteria();
$criteria->select = '*';
$criteria->condition = 't.user_id=:id';
$criteria->params = array(':id'=>Yii::app()->user->id);
$criteria->with = array('user'=>array('joinType'=>'inner join', 'alias' => 'user'));
$criteria->with = array('warengruppe'=>array('joinType'=>'inner join', 'alias' => 'warengruppe'));
$criteria->with = array('warengruppe', 'warengruppe.kabels');
$dataProvider = new CActiveDataProvider('Rabatt', array('criteria' => $criteria));
But when i call the Widget with following code i get the following error: "Trying to get property of non-object"
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'preisliste',
'type'=>'striped bordered condensed',
'dataProvider'=>$dataProvider,
'columns'=>array(
array('header'=>'Typ', 'value' => '$data->warengruppe->name'),
array('header'=>'Kabel', 'value' => '$data->warengruppe->kabels->name'),
array('header'=>'Rabatt (%)', 'value' => '$data->wert * 100'),
array('header'=>'Preis(ohne Rabatt)', 'value' => '$data->warengruppe->kabels->preis'),
//array('header' => 'Preis', 'value' => '$data->wert*$data->warengruppe->kabels->preis'),
),
)); ?>
I need to display a line for every cable for a specific user.
There has to be an error somewhere but i can’t seem to find it.
Thanks in advance
Edit:
Select r.wert, k.name as ‘kabelname’, w.name
from tbl_rabatt r
INNER JOIN tbl_user u on r.user_id=r.id
INNER JOIN tbl_warengruppe w on r.warengruppe_id=w.id
INNER JOIN tbl_kabel k on k.warengruppe_id=w.id
where u.id=1;
this would be the correct SQL statement, if that helps anyone