Yii Framework Forum: Help needed with relations describing an auto join - Yii Framework Forum

Jump to content

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

Help needed with relations describing an auto join Rate Topic: -----

#1 User is offline   mrac 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-February 10

Posted 18 February 2010 - 07:16 AM

Hi,

I''ve got some trouble describing the relations for my class :

I have a class named "Category" which is used to store categories and subcategories.
Subcategories are related to their parent category through a parent_category_id which is the id (in the same Category table) of the parent category.

I don't know how to overload the relations method in the model class.

I tried so far :

public function relations()
{
return array(
'subcategories'=>array(self::HAS_MANY,'Category','parent_category_id'),
'parentcategory'=>array(self::BELONGS_TO,'Category','id'),
);
}
but I'm not sure that this is the right way to do : when I try to display the parent category name in a view, using $data->parentcategory->name, I got the category name (and not the parent category name)

Any clue/advice ?

thks in advance and sorry for the bad english

Marc
0

#2 User is offline   Daniel M 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 19-January 10
  • Location:Germany, Saarland

Posted 18 February 2010 - 08:13 AM

It would be easier to help you if you post a description of your database table :)
Utinam ea res tibi usui fuerit!
0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 February 2010 - 08:38 AM

Try this:

public function relations()
{
  return array(
    'subcategories'=>array(self::HAS_MANY,'Category','parent_category_id'),
    'parentcategory'=>array(self::BELONGS_TO,'Category','parent_category_id'),
  );
}

0

#4 User is offline   mrac 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-February 10

Posted 18 February 2010 - 03:25 PM

Thank you for your answers !

@Mike, your solution works perfectly ... I should have guessed (or tried) that by myself <_<

@Daniel : here is the description (one table ...nothing simpler !)
Posted Image
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