在 Doctrine (Symfony2) 的 ManyToMany 连接表中有附加列 [英] have additional column in ManyToMany join table in Doctrine (Symfony2)

查看:18
本文介绍了在 Doctrine (Symfony2) 的 ManyToMany 连接表中有附加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体 User 和 Group ,它们具有 ManyToMany 关系.关系创建为具有两列 user_idgroup_id 的分隔表(称为 user_group).

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.

我想再添加一个字段 addedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP 到表 user_group.我不在乎学说会不会看到这个列,我可以在需要的时候通过 SQL 选择它.

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.

这可以通过教义配置来完成吗?如果是,如何?我知道我可以只在 DB 中添加列,但是每次生成迁移差异时都会有 SQL 语句来删除此列.

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.

应用:Symfony 2.5,Doctrine 2.4.

App: Symfony 2.5, Doctrine 2.4.

关系在配置中定义为 User.php:

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;

推荐答案

不,你不能,要在你的联结表中有 ManyToMany 关联和附加列,你需要重写你的映射您的用户和组实体之间的连接实体如下

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

团体实体相同

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

现在您可以将实体设置为

Now you can set your entities as

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

}

集团实体

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

}

UserHasGroups 实体

/**
 * 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');
    }

}

现在您已经映射了您的实体,您可以让您的 UserHasGroups 实体通过提供用户和组值(如 findBy,findAll 等)使用存储库方法,或者如果您有用户对象您可以直接获取包含该用户关联集合的 UserHasGroups 对象

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

这篇关于在 Doctrine (Symfony2) 的 ManyToMany 连接表中有附加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆