2

Image describing the tables -> https://i.stack.imgur.com/ki2YP.jpg

Each of the tables is a model.

Main model which shows through CGridView is "RegularTask".

Now I need it to display fields from "YearlyTask" in the same row.

"hp_id" and "up_id" are FK (foreign keys) in both tables.

I tried to set the relations() in the RegularTask model like this:

'arp' => array(self::BELONGS_TO, 'YearlyTask', 'hp_id, up_id'),

Then I try to display the "is_sent" and "is_reported" fields from YearlyTask by using "arp.is_sent" and "arp.is_reported", but nothing shows up (not even error). While data from RegularTask displays normally.

What am I doing wrong?

Here is a snippet from the dataprovider..

<?php
$dataProvider=new CActiveDataProvider('RegularTask', array(
    'criteria'=>array(
        'condition'=>'t.id_id=' . $model->id,
        'order'=>'t.created DESC',
        'with'=>array('arp'),
    ),
    'pagination'=>array(
        'pageSize'=>10,
    ),
));

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'comment',
        'arp.is_sent'
    ),
));
?>
tshepang
  • 12,111
  • 21
  • 91
  • 136
Xatep
  • 353
  • 1
  • 5
  • 11
  • can you show your data provider and a snippet from where you are trying to access the related values? – ldg Sep 30 '11 at 00:13
  • @ldg I updated the description and added code snippet you were asking for. – Xatep Sep 30 '11 at 02:41
  • your view code looks ok, so I suspect it's something about the relation - does this relation work outside of the widget? If not, try changing the relationship type. Note that if you are using real foreign keys (InnoDB), you can try having gii create the relation for you. – ldg Sep 30 '11 at 03:59

2 Answers2

2

Foreign key in one table must be primary key in linked table. If you look into SQL statement created by Yii with your structure, you will see something like this (simplified):

SELECT `t`.*, `arp`.* FROM `RegularTask` `t` LEFT OUTER JOIN `YearlyTask` `arp` ON (`t`.`hp_id`=`arp`.`id`) AND (`t`.`up_id`=`arp`.`id`)

As you can see, what it looks for is that both RegularTask.hp_id and RegularTask.up_id have same value as YearlyTask.id which isn't correct. You have several solutions. One of them is create new column in RegularTask like yt_id which will be foreign key for YearlyTask.id and in my opinion it's the best solution.

Otherwise you can delete your primary key column id in RegularTask table and make hp_id and up_id as complex primary key. In this case you can use the way you tried before, but as i don't know your full db structure i can't guarantee that it's good solution.

Johnatan
  • 1,268
  • 8
  • 16
-1

have you tried using this style:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'comment',
        array(
            'header'=>'Sent',
            'type'=>'raw',
            'value'=>'$data->arp->is_sent',
        )
    ),
));

perhaps not the most elegant way, but might be of help

ZaQ
  • 1,256
  • 11
  • 12
  • fyi, it is true that style might make it easier to debug, but it's not the problem here. – ldg Sep 30 '11 at 20:16
  • 2
    for value, i recommend to use CHtml::value($data,'arp.is_sent') it will avoid an empty relation errors – RusAlex Oct 06 '11 at 14:11