Monday, 15 March 2010

php - Additional fields in ORM-ManyToMany relation -


I have tables with data: Users and groups There are many people in my database connection between these two entities, because many Users can be in multiple groups and many groups can have many users. It is simple. I have created this code and it works:

User Unit:

  / ** * @ORM \ unit * @ORM \ Table = name = "user") * Expands / class user baseUser {/ ** * @ORM \ ID * @ORM \ column (type = "integer") * @ORM \ generated value (strategy = "auto ") * / Protected $ id; / ** * @ORM \ ManyToMany (targetEntity = "Group", inversedBy = "users") * @ORM \ JoinTable (name = "users_in_groups") * $ / protected groups; }  

Group unit:

  / ** * @ORM \ unit * @ORM \ table (name = "groups ") * / Class group {/ ** * @ORM \ id * @ORM \ column (type =" integer ") * @ORM \ generated value (strategy =" auto ") * / protected $ id; / ** * @ORM \ ManyToMany (targetEntity = "user", mapped = "group") * / protected $ user; }  

Symphony created three tables: user , groups and users_in_groups . Everything works great, but I want to set different permissions for different users of each group. For example: User1 is an Administrator in Group1 and standard members etc. in Group2 The best solution can be to insert a new area for users_in_groups but this table was created by the @ORM \ JoinTable function and I do not know how I can do it. Do you know the simplest solution to this problem?

The easiest way to do this is that I have found

User Unit :

  / ** * @ORM \ Entity * @ORM \ Table (name = "users") * / class extends user baseUser {/ ** * @ ORM \ Id * @ORM \ column (type = "integer") * @ORM / generated values ​​(strategy = "auto") * / protected $ id; / ** * @ORM \ OneToMany (targetEntity = "UserGroup", mapped = "user") * * / protected $ usergroup; }  

Group unit:

  / ** * @ORM \ unit * @ORM \ table (name = "groups ") * / Class group {/ ** * @ORM \ id * @ORM \ column (type =" integer ") * @ORM \ generated value (strategy =" auto ") * / protected $ id; / ** * @ORM \ OneToMany (targetEntity = "UserGroup", mapped = "group"}) * * / protected $ usergroup; }  User group unit:   
  / ** * @ORM \ unit * @ORM \ table (name = " User_group ") * / class UserGroup {/ ** * @ORM \ id * @ORM \ column (type =" integer ") * @ORM \ generated value (strategy =" auto ") * * @ integer $ id * / protected $ Id; / ** * @ORM \ ManyToOne (targetEntity = "user", inversedBy = "usergroup") * @ORM \ JoinColumn (name = "user_id", referenced column name = "id") * * / protected $ user; / ** * @ORM \ ManyToOne (targetEntity = "Group", inversedBy = "usergroup") * @ORM \ JoinColumn (name = "group_id", referenced column name = "id") * * / protected $ group; // extra field, gator, setters, _Construct, __toString}  

No comments:

Post a Comment