简体   繁体   English

学说一对多关系和多对一关系

[英]Doctrine one-to-many relationship AND many-to-one

I have two tables. 我有两张桌子。 I want to set up a one-to-many relationship, but also a many-to-one relationship. 我想建立一对多的关系,但也要建立一对多的关系。

A Page can have one Background - this is the background of the page. 页面可以有一个背景-这是页面的背景。

A Page can also have many Backgrounds - this is a collection of user-uploaded backgrounds from which one will be chosen for the first relationship. 页面也可以具有许多背景-这是用户上传背景的集合,将从中为第一关系选择一个背景。

In other words, a user selects a background from a bunch of predefined backgrounds, or one of many backgrounds he has uploaded to try out. 换句话说,用户从一堆预定义的背景中选择一个背景,或者从他上传的许多背景中选择一个来进行试用。

edit: When deleting a background, I want all pages with that background_id to have background_id set to null. 编辑:删除背景时,我希望所有具有该background_id的页面的background_id设置为null。 When deleting a page, I want all the custom backgrounds belonging to that page to be deleted. 删除页面时,我希望删除属于该页面的所有自定义背景。

Whilst doctrine and symfony allow the above configuration, when deleting the page Doctrine ignores cascade="{remove}" on the Backgrounds property entirely, and of course an exception is raised when trying to delete the Page before deleting it's custom Backgrounds. 尽管主义和symfony允许进行上述配置,但是在删除页面时,Doctrine会完全忽略Backgrounds属性上的cascade =“ {remove}”,当然,在删除页面的自定义背景之前尝试删除该页面时会引发异常。

What am I doing wrong? 我究竟做错了什么?

class Background
{
/**
 * @var string
 *
 * This attribute is for user uploaded backgrounds.
 *
 * @ORM\ManyToOne(targetEntity="Page",inversedBy="customBackgrounds")
 * @ORM\JoinColumn(name="page_id",referencedColumnName="id")
 */
protected $page;

/**
 * @var string
 *
 * This field helps admins to gauge popularity
 *
 * @ORM\OneToMany(targetEntity="Page",mappedBy="background")
 */
protected $pages;
}


class Page
{
/**
 * @var string
 *
 * @ORM\ManyToOne(targetEntity="Background",inversedBy="pages")
 * @ORM\JoinColumn(name="background_id",referencedColumnName="id", nullable=true, onDelete="SET NULL")
 */
protected $background;

/**
 * @ORM\OneToMany(targetEntity="Background", mappedBy="page", cascade={"remove"})
 * @ORM\OrderBy({"id" = "ASC"})
 */
protected $customBackgrounds;
}

try @ORM\\JoinColumn(name="page_id",referencedColumnName="id", onDelete="SET NULL") and do a schema update. 尝试@ORM\\JoinColumn(name="page_id",referencedColumnName="id", onDelete="SET NULL")并进行架构更新。

EDIT : The problem is at the database level not at doctrine's, the cascade="remove" takes care of that, the foreign key for page_id however, stays, the onDelete indicates that if a column with that foreign relationship is deleted, set the field to "value" this case its null. 编辑 :问题出在数据库级别,而不是教义级别,级联=“删除”解决了这一问题,page_id的外键仍然存在,onDelete表示如果删除了具有该外关系的列,请设置字段以“值”此情况下的null。 if you --dump-sql before schema update'ing you would see the query addition, something along the lines of " ON DELETE SET * " 如果在架构更新前使用--dump-sql,则会看到查询添加,类似于“ ON DELETE SET *”

More Info can be found Here: http://www.techonthenet.com/oracle/foreign_keys/foreign_null.php 在此处可以找到更多信息: http : //www.techonthenet.com/oracle/foreign_keys/foreign_null.php

唯一可以做的就是在实体中使用事件侦听器,并在删除页面时将其背景设置为null或另一个ID。

/**
 * @ORM\OneToMany(targetEntity="Background", mappedBy="page", orphanRemoval=true)
 * @ORM\OrderBy({"id" = "ASC"})
 */
protected $customBackgrounds;

Should do the trick ( Doctrine - Orphan Removal ) 应该做的trick俩( 学说-孤儿撤除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM