Jump to content
Larry Ullman's Book Forums

Yii Relational Active Record Bug?


Edward
 Share

Recommended Posts

According to the relationship rules in the Yii Book i have some buggy situations?

 

Relations for Item Model

 

'itemShipping' => array(self::HAS_MANY, 'ItemShipping', 'id'),

(This doesn't work and returns an empty array, the thread value of the array is the relation in this model)

 

'itemShipping' => array(self::HAS_MANY, 'ItemShipping', 'item_id),

(This works returning an array of objects using the item_id from the item_shipping table related attributed)

 

But in the situation where

 

'user' => array(self::BELONGS_TO, 'User', 'user_id'),

(This works using the user id from this model)

 

So reading the Yii Book there is some subtle confusion in these relations as it doesn't work for the relationship in this model in particular cases. I think i can see what's going on, you can try testing it for yourself.

 

Therefore,

 

self::HAS_ONE or self::HAS_MANY = Related attribute from other model

self::BELONGS_TO = Related attribute in this model

 

Edward,

Link to comment
Share on other sites

Hey Edward,

 

I doubt there's a bug in the framework, more likely I wasn't sufficiently clear. In order for me to make sure I'm not adding to the confusion, could you post your applicable table names and PK/FK columns?

Link to comment
Share on other sites

No offence but I found the book to be a little misleading by the way it was written on this rare occasion that is why i got into this mess. I have marked the statements below in red, you say this model which is the Comment's model in your situtation. Well what if the foreign keys are not in this model then it wouldn't always be this model. Sometimes you have the primary keys in the model you are making the relations for and foreign keys in the other. I actually never read the Yii Definitive Guide as i just read it in your book, but the way they have written it makes it clearer when they state foreign keys. So do you see how the word "this" could throw people off?

 

Yii Book explanation:

 

# protected/models/Comment.phppublic function relations() {
    return array(        'page' => array(self::BELONGS_TO, 'Page', 'page_id'),        'user' => array(self::BELONGS_TO, 'User', 'user_id'),

); }

This method returns an array. Each array index is the relation’s name. The relation’s name, is a made up value, that should be obviously meaningful.

Each value is another array, starting with the relationship type, followed by the related model, followed by the attribute in this model that relates to that model (i.e., the foreign key to that model’s primary key). The above code indicates that Comment belongs to Page via the page_id attribute. In other words, each com- ment belongs to a page, and the association is made through page_id. The same relationship exists with User.

 

Yii Definitive guide explanation:

 

Declaring relationship in AR involves overriding the relations() method of CActiveRecord. The method returns an array of relationship configurations. Each array element represents a single relationship with the following format:

'VarName'=>array('RelationType', 'ClassName', 'ForeignKey', ...additional options)

where VarName is the name of the relationship; RelationType specifies the type of the relationship, which can be one of the four constants: self::BELONGS_TO, self::HAS_ONE, self::HAS_MANY and self::MANY_MANY; ClassName is the name of the AR class related to this AR class; and ForeignKey specifies the foreign key(s) involved in the relationship. Additional options can be specified at the end for each relationship (to be described later).

Link to comment
Share on other sites

No offense taken. I don't see how my writing is unclear on this, but I appreciate that you do and I'll revisit that passage. The important thing is that you have a good understanding of it now, which I assume you do, thanks to the Definitive Guide?

  • Upvote 1
Link to comment
Share on other sites

Let me try to explain again, how could it be a foreign key attribute if the foreign key attribute was in the other related model and the primary key was in this model. I thought your explanation meant you had to pick the key from the model you were creating relationships for only as you stated this model. You could of said this or the other model, that would make it more understandable. Again sorry i don't mean to be rude, its just i had read that more than once and got confused with it twice.

 

I have managed to get my relationional query fetching the correct data most of it was tabular data which returned an array of objects, something i love to deal with now, so my relationships seem to be working. :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...