开发者

Question about many to many relationship: complete iteration

开发者 https://www.devze.com 2023-02-05 08:21 出处:网络
I have a many-to-many relationship between two tables. I would like to show the elements of a table with the corresponding elements of the other table.

I have a many-to-many relationship between two tables.

I would like to show the elements of a table with the corresponding elements of the other table.

For example:

user1->group2
       group5
user2->group1
       group3
       group4
...

In an easy and smart way, of course. I'm using php, symfony and propel 1.4.

Any advice or tutorial?

EDIT:

I used a join and I get the objects from the exchange table, so if I iterate it I get:

user1->group2
user1->group5
user2->group1
user2->group3
user2->开发者_开发问答;group4

But I'm wondering how could i get the group objects from the user objects.

Regards

Javi


Well.. would a simple join suffice?


If you have defined the relationshop between your user and your group table in the propel schema, the User objects will have a getGroups() method. Call this from one User and you get a list of all groups this user is joined with. You can do this without doing the join yourself but it will be less efficient, as it will do one extra database query per user. If you do the join when you query for the users, the groups will be cached too.

0

精彩评论

暂无评论...
验证码 换一张
取 消