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

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

问题描述

我有两个实体,用户和组的关系是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.

我想在表user_group中再添加一个字段addedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP.我不在乎该学说不会看到此列,我可以在需要时通过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.

这可以通过原则配置完成吗?如果是,怎么办? 我知道我可以在数据库中添加列,但是每次生成迁移差异时,都会有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.

App:Symfony 2.5,Doctrine 2.4.

App: Symfony 2.5, Doctrine 2.4.

关系在配置中与User.php相同:

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;

推荐答案

不可以,要在联结表中具有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
 * @ORM\Table(name="user_table_name")
 * @ORM\Entity
 */
class User
{
    /**
     * @ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\UserHasGroups", mappedBy="users",cascade={"persist","remove"} )
     */
    protected $hasGroups;

}

组实体

/**
 * Group
 * @ORM\Table(name="group_table_name")
 * @ORM\Entity
 */
class Group 
{
    /**
     * @ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\UserHasGroups", mappedBy="groups",cascade={"persist","remove"} )
     */
    protected $hasUsers;

}

UserHasGroups实体

/**
 * UserHasGroups
 * @ORM\Table(name="user_groups_table_name")
 * @ORM\Entity
 */
class UserHasGroups
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\Group", cascade={"persist"}, fetch="LAZY")
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
     */
    protected $groups;

    /**
     * @ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\User", cascade={"persist","remove"} ,inversedBy="medias", fetch="LAZY" )
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id",nullable=true)
     */
    protected $users;


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

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

}

现在,您已经映射了实体,可以通过提供用户和组值(例如findBy,findAll等)来使UserHasGroups实体使用存储库方法,或者如果您具有用户对象,则可以直接获取包含以下内容的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天全站免登陆