Situation
I have two entities User and Group with relation ManyToMany. Relation is created as separated table (called user_group
) with two columns user_id
and group_id
.
Problem
I want to add one more field addedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP
to table user_group
. I do not care that doctrine will not see this column, I could select it via SQL when I need it.
Question
Can this be done via doctrine configuration? If yes, how? I know I can just add column in DB but then everytime I generate migration diff there would be SQL statement to delete this column.
Relative info
App: Symfony 2.5, Doctrine 2.4.
Relation is defined in configuration as is User.php:
/**
* @ORM\ManyToMany(targetEntity="RAZ\UserBundle\Entity\Group")
* @ORM\JoinTable(name="user_group",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
No you can't, to have
ManyToMany
association and additional column in your junction table you need to rewrite your mapping as to have a junction entity between your user and group entity as belowFor user your mapping will be like
Same for group entity
Now you can set your entities as
User Entity
Group Entity
UserHasGroups Entity
Now you have mapped your entities you can have your
UserHasGroups
entity use repository method by provide user and group values likefindBy,findAll
etc or if you have user object then you can directly get theUserHasGroups
object which contains the collection of associations for that user