Doctrine 2: How to handle join tables with extra columns

六月ゝ 毕业季﹏ 提交于 2019-11-27 00:37:40

问题


How do I setup a join table with extra columns, or a many-to-many association with additional properties, in Doctrine 2?


回答1:


First off, let me explain that this does not exist:

A join table (also known as a junction table or cross-reference table) is a table that links 2 (or more) other tables together within the same database by primary key. This means that a join table will only contain foreign keys, there is no place for these extra columns.

So when you need extra columns in such a table, it is no longer just a "link" between other tables, but becomes a real table on its own!

In terms of Doctrine 2, you no longer have a many-to-many association between 2 entities, but get a one-to-many/many-to-one association between 3 entities.

Continue reading here for more details explanations:

  • Doctrine 2: How to handle join tables with extra columns
  • More on one-to-many/many-to-one associations in Doctrine 2



回答2:


In the second article, I suggest a minor update. Instead of the full event, use a LifecycleCallback within the entity itself:

/**
 * @ORM\Entity
 * @ORM\Table(name="jobs”)
 * @ORM\HasLifecycleCallbacks
 */
class Job
{
    // ...

    /**
     * @ORM\PreRemove
     */
    public function preRemoveCallback()
    {
        $this->setPerson(null);
        $this->setCompany(null);
    }
}


来源:https://stackoverflow.com/questions/18655286/doctrine-2-how-to-handle-join-tables-with-extra-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!