I find the documentation very poor when it comes to explaining the creation of relationships between entities. So, i'll have to ask for help to my fellow StackExchangers. So, i'm trying to build the following cases:
Case 1
A User
belongs to one or more Group
, and a Group
can have many Permission
. A User
also can have a Permission
.
Case 2
A Ticket
has a Category
, multiple Tag
and multiple Comment
.
Thanks in advance!
Try this:
You will have One to Many relation between
User
andGroup
.. ThetargetEntity
is the path to the Entity that you want have a relationship with, themappedBy
is the variable from theGroup
Entity.cascade
meansUser
can add toGroup
and remove fromGroup
Class Group {
This is the reserve side of the relationship..
targetEntity
should have the path back to the parent entity, which isUser
in this case.inversedBy
is the variable from theUser
Entity.JoinColumn
is just telling Doctrine what to join on, this is automatically done if you don't set it yourself.Sure thing. First thing to understand is that there is no "one way" to do this. Doctrine gives a lot of flexibility in terms of how you define the relationship - even if multiple definitions produce the exact same DDL (and this is important to understand - some of the mapping choices only effect the object-side of the ORM, not the model-side)
Here's your Users/Groups/Permissions example, which are actually all many-to-many associations (I excluded all non-relevant but required code, like PK column definition)
If you have questions about what's going on here, let me know.
Now, to your second example
As before, let me know if you want any of this explained.
P.S. None of this was actually tested, I just kinda banged it out in my IDE real fast. There might be a typo or two ;)