Symfony:ManyToMany 表额外的列 [英] Symfony: ManyToMany table extra columns

查看:24
本文介绍了Symfony:ManyToMany 表额外的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 UserHouse 的多对多表,称为 user_house.除了两列:user_id 和 house_id,我还想添加 3 列:例如 action、created_at、updated_at.我该怎么做?

I have a many to many table for User and House, called user_house. Instead of just two columns: user_id and house_id, i want to add 3 more: eg action, created_at, updated_at. How can I do this?

我找不到任何相关文档.

I cannot find any relevant docs on this.

以下只是创建一个单独的表,其中包含两列.

The following just creates a separate table with two columns in it.

class User extends EntityBase
{
    ...
    /**
     * @ORMManyToMany(targetEntity="AppBundleEntityHouse")
     */
    protected $action;

基本上,我想要实现的是:

Basically, what I want to achieve is:

user_house表中,user_idhouse_idaction的组合应该是唯一的.

in the user_house table the combination of user_id, house_id, action should be unique.

当用户点击房子上的视图"时,user_house 表会更新为some user_idsome house_idviewnow(), now()

when a user clicks a "view" on a house, user_house table gets updated with some user_id, some house_id, view, now(), now()

当用户点击房子上的赞"时,user_house 表会更新为some user_idsome house_idlikenow(), now()

when a user clicks a "like" on a house, user_house table gets updated with some user_id, some house_id, like, now(), now()

当用户点击某个房子的请求呼叫"时,user_house 表会更新为some user_idsome house_idcontact, now(), now()

when a user clicks a "request a call" on a house, user_house table gets updated with some user_id, some house_id, contact, now(), now()

有人能指出我正确的方向吗?谢谢!

Could someone point me in the right direction? Thanks!

推荐答案

您需要通过以下方式打破 ManyToManyOneToManyManyToOne 的关系引入一个名为 UserHasHouses 的连接实体,这样您就可以向连接表 user_house

You need to break your ManyToMany relation to OneToMany and ManyToOne by introducing a junction entity called as UserHasHouses, This way you could add multiple columns to your junction table user_house

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

}

房屋实体

/**
 * Group
 * @ORMTable(name="house")
 * @ORMEntity
 */
class House
{
    /**
     * @ORMOneToMany(targetEntity="NameSpaceYourBundleEntityUserHasHouses", mappedBy="houses",cascade={"persist","remove"} )
     */
    protected $hasUsers;

}

UserHasHouses 实体

/**
 * UserHasHouses 
 * @ORMTable(name="user_house")
 * @ORMEntity
 */
class UserHasHouses 
{

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

    /**
     * @ORMManyToOne(targetEntity="NameSpaceYourBundleEntityHouse", cascade={"persist"}, fetch="LAZY")
     * @ORMJoinColumn(name="house_id", referencedColumnName="id")
     */
    protected $houses;

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


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


    /**
     * @var DateTime
     * @ORMColumn(name="updated_at", type="datetime")
     */
    protected $updatedAt;
     //... add other properties
    public function __construct()
    {
        $this->createdAt= new DateTime('now');
    }

}

在多对多中有额外的列Doctrine (Symfony2) 中的连接表

这篇关于Symfony:ManyToMany 表额外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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