Hi, I’m using an intermediate “join” table to establish a MANY_MANY relationship for the first time, and I find that when I use the relation name in a query, it is not recognized. Here’s my scenario:
I have a User model and a Project model, and the intermediate join model is the UserProject model. The UserProject model just has the user_id and project_id. The relation names should be ok:
But the join is not made, the relation name “users” is not recognized even though it’s declared in the Project model. Can someone tell me what I’m missing?
The UserProject model joins the user table and the project table in the database, using only the id’s of the two tables. It has only the fields user_id and project_id. I have not modified the relationships in either the User model or the Project model.
I don’t think the ‘with’ in the criteria property joins the two table. You can join the table by adding ‘join’ statement. You can look this up for more information:
What I was trying to do was list all of the projects that belong to one artist, using a CActiveDataProvider in the ProjectController’s actionIndex(). I just needed to add the ‘together’=>true parameter, like so:
Thanks everybody, my original question was misleading. In fact the users relation works without specifying it in the 'with" parameter. I was unable to use ‘users.id’ without the ‘together’=>true parameter.
Nope, it’s not working as I would like. With the code above, it only returns a single user, when there are multiple users associated with a given project. Does anyone know what I’m still missing?
Well in the end, I found a solution, but it is not what I had expected. In order to obtain an ordered list of users belonging to a single project, I did this:
The foreach loop uses the "users" relation to obtain the associated users (artists).
This is my first experience using an intermediate join table to establish a MANY_MANY relation. There must be an easier way, I’m not done with this yet.