Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

symfony - have additional column in ManyToMany join table in Doctrine (Symfony2)

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:

/**
 * @ORMManyToMany(targetEntity="RAZUserBundleEntityGroup")
 * @ORMJoinTable(name="user_group",
 *      joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORMJoinColumn(name="group_id", referencedColumnName="id")}
 * )
 */
protected $groups;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 below

For user your mapping will be like

        OneToMany
      ------------>
User          UserHasGroups
      <------------
        ManyToOne

Same for group entity

        OneToMany
       ----------->
Group          UserHasGroups
       <-----------
        ManyToOne

Now you can set your entities as

User Entity

/**
 * User
 * @ORMTable(name="user_table_name")
 * @ORMEntity
 */
class User
{
    /**
     * @ORMOneToMany(targetEntity="NameSpaceYourBundleEntityUserHasGroups", mappedBy="users",cascade={"persist","remove"} )
     */
    protected $hasGroups;

}

Group Entity

/**
 * Group
 * @ORMTable(name="group_table_name")
 * @ORMEntity
 */
class Group 
{
    /**
     * @ORMOneToMany(targetEntity="NameSpaceYourBundleEntityUserHasGroups", mappedBy="groups",cascade={"persist","remove"} )
     */
    protected $hasUsers;

}

UserHasGroups Entity

/**
 * UserHasGroups
 * @ORMTable(name="user_groups_table_name")
 * @ORMEntity
 */
class UserHasGroups
{

    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="NameSpaceYourBundleEntityGroup", cascade={"persist"}, fetch="LAZY")
     * @ORMJoinColumn(name="group_id", referencedColumnName="id")
     */
    protected $groups;

    /**
     * @ORMManyToOne(targetEntity="NameSpaceYourBundleEntityUser", cascade={"persist","remove"} ,inversedBy="medias", fetch="LAZY" )
     * @ORMJoinColumn(name="user_id", referencedColumnName="id",nullable=true)
     */
    protected $users;


    /**
     * @var DateTime
     * @ORMColumn(name="added_on", type="datetime")
     */
    protected $addedOn;

    public function __construct()
    {
        $this->addedOn= new DateTime('now');
    }

}

Now you have mapped your entities you can have your UserHasGroups entity use repository method by provide user and group values like findBy,findAll etc or if you have user object then you can directly get the UserHasGroups object which contains the collection of associations for that user


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...