Yii Framework Forum: Model many-to-one relations function - Agile Yii book ^^ - Yii Framework Forum

Jump to content

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

Model many-to-one relations function - Agile Yii book ^^

#1 User is offline   digitalzombie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 19-August 10

Posted 27 August 2010 - 03:15 PM

So, in this book chapter 6. We have 3 tables now.

What's confusing is the relation function.

Project Model's relations function is defined as:
public function relations()
{
    return array(
    'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
    'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
    );
}


The Issue table relationship to Project table is one to many. But! The author and the code generator left it as:
Issue Model:
public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
        'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
        'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
    );
}


The only relations we have are 3 foreign keys. When I looked up Yii's Documentation there's a HAS_ONE AR option.

So therefore my question is:
Should it need to defined it? If not why? Redundant for some reason?

If I do need to state this relationship is this it (am I doing it right):
    'project_id' => array(self::HAS_ONE, 'Project', 'project_id'),

0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 27 August 2010 - 05:35 PM

View Postdigitalzombie, on 27 August 2010 - 03:15 PM, said:

So, in this book chapter 6. We have 3 tables now.

What's confusing is the relation function.

Project Model's relations function is defined as:
public function relations()
{
    return array(
    'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
    'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
    );
}


The Issue table relationship to Project table is one to many. But! The author and the code generator left it as:
Issue Model:
public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
        'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
        'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
    );
}


The only relations we have are 3 foreign keys. When I looked up Yii's Documentation there's a HAS_ONE AR option.

So therefore my question is:
Should it need to defined it? If not why? Redundant for some reason?

If I do need to state this relationship is this it (am I doing it right):
    'project_id' => array(self::HAS_ONE, 'Project', 'project_id'),



A HAS_MANY/HAS_ONE relationship corresponds to a BELONGS_TO relationship in the model that represents the other table. You don't have to declare relations in both models, just in the model you use as primary in your query.

Read more about Relational AR in the guide.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   digitalzombie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 19-August 10

Posted 27 August 2010 - 07:43 PM

View Posttri, on 27 August 2010 - 05:35 PM, said:

A HAS_MANY/HAS_ONE relationship corresponds to a BELONGS_TO relationship in the model that represents the other table. You don't have to declare relations in both models, just in the model you use as primary in your query.

Read more about Relational AR in the guide.

/Tommy


Thank you!
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