0

Tables:

tbl_user(id, name)
tbl_group(id, name);
tbl_user_group(user_id, group_id);

If there were all three classes generated User,Group,UserGroup. How should i setup the relations in User or Group so that they are mapped through the user_group table?

I'm using Doctrine 1.2

Keeper Hood
  • 594
  • 5
  • 17

1 Answers1

1

It's up to you to define their associations, purely because only you will know how you want your application to work. Can a user have more than one group? Only one? If a group is deleted, will all associated users be deleted? These are the questions you need to ask yourself.

If you follow the example on Doctrine's documentation for Many-to-Many relationships (which coincidentally covers exactly what you need), you'll get there.

hohner
  • 11,498
  • 8
  • 49
  • 84
  • Thanks. But i don't understand what is "Group as Groups" supose to represent, an alias ? – Keeper Hood Feb 17 '13 at 11:35
  • `Group` is the name of the entity you're associating with, and `Groups` is the class parameter which provides access to all of that User's associations for groups. So in your code, you'd do: `$user->Groups[0]->name` – hohner Feb 17 '13 at 11:39
  • Do i have to join those relations somehow when doing Doctrine_Query::create() or are they lazy-loaded? – Keeper Hood Feb 17 '13 at 11:44
  • They'll be done for you (with lazy-loading). You don't have to manually specify JOIN queries, etc. - that's the point of Doctrine – hohner Feb 17 '13 at 11:47
  • But one of the queries in http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/working-with-models.html, in the examples looks like it must have ->leftJoin('u.Email e'). – Keeper Hood Feb 17 '13 at 12:03
  • Doing this is _optional_. You can either use Doctrine to create the relationships for you (and access it in your code `$user->Groups[0]->name;`), or use DQL and do it yourself. I don't see the point in specifying all the queries yourself - you're using Doctrine to make your life easier and more efficient. – hohner Feb 17 '13 at 12:14