Yii Framework Forum: Small Self Relation Trouble - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Small Self Relation Trouble Rate Topic: -----

#1 User is offline   CrazyCat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 30-May 13
  • Location:Paris, France

Posted 16 October 2013 - 10:19 AM

Hello there,

I've a small trouble with a self related table.
The (simplified) structure of the table is:
id int(11) PK
name varchar(50)
parentId int(11)


In my model, I've:
public function relations()
{
   return array(
      'isChildOf' => array(self::BELONGS_TO, 'MyTable', 'id'),
      'isParentOf' => array(self::HAS_MANY, 'MyTable', 'parentId'),
   );
}


In my admin view, I want to display the parent name:
<?php $this->widget('CGridView', array(
   'id'=>'mytable-grid',
   'dataProvider'=>$model->search(),
   'columns'=>array(
      'id',
      'name',
      array(
         'name' => 'parentId',
         'value' => '$data->parentId'
      ),
      array(
         'header' => 'litteral',
         'value' => '$data->isChildOf->name'
      ),
)); ?>

When displaying my admin, 'parentId' is ok, but 'litteral' displays the name of the current element, not his father name...

I think I make something wrong, but what ?
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 614
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 16 October 2013 - 11:41 AM

Both BELONGS_TO and HAS_MANY uses the same column as fk. Just change id to parentId in isChildOf.
Don't be a dick.
0

#3 User is offline   CrazyCat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 30-May 13
  • Location:Paris, France

Posted 16 October 2013 - 02:32 PM

View Postnineinchnick, on 16 October 2013 - 11:41 AM, said:

Both BELONGS_TO and HAS_MANY uses the same column as fk. Just change id to parentId in isChildOf.

Ok, I'll try tomorrow, I think I've tested but I've done a lot of combinations and peharps I miss the good one :)
0

#4 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 16 October 2013 - 09:02 PM

try this, using 'parent' and 'children' for relation is much straight-forwards than 'isChildOf' and 'isParentOf'

public function relations()
{
   return array(
      'parent' => array(self::BELONGS_TO, 'MyTable', 'parentId'),
      'children' => array(self::HAS_MANY, 'MyTable', 'parentId'),
   );
}

I enjoy the Yii.sy coding life here.
0

#5 User is offline   CrazyCat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 30-May 13
  • Location:Paris, France

Posted 17 October 2013 - 01:47 AM

Hello there, and thanks for your help.

The trouble in the code I gave you was the fk, and before posting I'd tried with the same reference, I gave up because I had errors (trying to get properties of a non-object).
And then, THE solution: the error was in the template, some items haven't any parent, then I've to do:
array(
         'header' => 'litteral',
         'value' => '($data->parentId!=0)?$data->isChildOf->name:""'
      ),

Just a stupid error, sorry for the time you spent :)

Regards
0

#6 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 17 October 2013 - 03:46 PM

not sure if this is the right way to do it.

      array(
         'header' => 'litteral',
         'value' => '$data->isChildOf->name'
      ),


since 'children' is supposed to be in has-many relationship while you are trying to display 'one'

i rather do this:

$children = $model->isChildOf;

if ($children){
   //children list here
}


I enjoy the Yii.sy coding life here.
0

#7 User is offline   CrazyCat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 30-May 13
  • Location:Paris, France

Posted 18 October 2013 - 02:33 AM

View Postrootbear, on 17 October 2013 - 03:46 PM, said:

not sure if this is the right way to do it.

      array(
         'header' => 'litteral',
         'value' => '$data->isChildOf->name'
      ),


since 'children' is supposed to be in has-many relationship while you are trying to display 'one'

i rather do this:

$children = $model->isChildOf;

if ($children){
   //children list here
}



No no :)
"isChildOf" is well nommed, it contains the id of the parent, or 0, so a record can only be child of one other record.
But I made a small mistake, it's HAS_ONE and not HAS_MANY in the relation.

I think I've peharps an inversion between HAS and BELONG, I'll verify that.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users