Add add attributes to relations?


I am quite new to Yii (switching from CodeIgniter) and would like to know if I can add attributes to relations.

Example: I have a User and I have a list of Skills. Now I want to assign Skills to a User by using a relationship. To assign a skill to a User I will use an assignment-Table if I understood this correctly.

Now I want to add an Attribute to this relationship to express how well the User is in this Skill.

Example: I have a User who is assigned to the Skill "speaks English". To express how well he speaks English I would like to add an Attribute to this relationship.

a) Is this possible? How would I do that?

B) I will have to search for “All Users having Skill “speaks English” with a rating of min. 3 of 5 points”. How would I do this in Yii?

Thanks for help.

I haven’t used this feature yet, but through was added for (more straightforward?) access to attributes of the association table.


Thanks for your fast reply.

I read through the "through" feature but Im not sure if I got it right so I would like to clarify this:


[*]I have a User table, a Skill table and a User_Skill_Assignment table

[*]The User_Skill_Assignment table has the two foreign keys and a column "quality"

[*]In the User-class (=Model) I define a relationship "HAS_MANY" to the Skill-class (=Model).


As far as I understood the "through" feature I can access another model (e.g. address) from the Skill-Model "through" the User-Skill-Address relationship. But where do I get the access to the attribute of the relationship between User and Skill?

You could give some short example? No need to write valid code though, just some idea.

Ok, what I understand now is that I have to use a Model to represent the relationship to add attributes, right?

Nobody had this requirement yet and could give me the best practice?

Yes, you will need to create a model for the join table as well.

Check if this thread can help you out. Bad choice since it concludes not to use through. (This post will give you a brief explanation on your way back to the description in the guide.) ;)

(As said, I still have to become familiar with the through mechanism)


Thanks for the link. I guess I will try to understand the "through" thing.

But it feels quite complicated for such a simple thing. Is it not a often needed feature?

Since you will need to create the third link table model anyway, you may consider the option to use that model as primary in your query and address two BELONGS_TO relations you’ve added to it.


You mean:

A <-> A_B <-> B

A BELONGS_TO A_B (using A as key)

B BELONGS_TO A_B (using B as key)


How can I echo an attribute of the related model directly? Like echo $a->b->attribute_of_b?