我有两个表。我想建立一种一对多的关系,但也要建立一种多对一的关系。
一个页面可以有一个背景——这就是页面的背景。
一个页面也可以有许多背景——这是用户上传的背景的集合,将从中选择一个作为第一段关系的背景。
换句话说,用户从一堆预定义的背景中选择一个背景,或者从他上传来尝试的许多背景中选择其中一个。
edit:当删除背景时,我希望所有具有该background_id的页面都将background_ids设置为null。删除页面时,我希望删除属于该页面的所有自定义背景。
虽然条令和符号允许上述配置,但在删除页面时,条令完全忽略Backgrounds属性上的cascade="{remove}",当然,在删除页面的自定义Backgrounds之前尝试删除页面时会出现异常。
我做错了什么?
class Background
{
/**
* @var string
*
* This attribute is for user uploaded backgrounds.
*
* @ORMManyToOne(targetEntity="Page",inversedBy="customBackgrounds")
* @ORMJoinColumn(name="page_id",referencedColumnName="id")
*/
protected $page;
/**
* @var string
*
* This field helps admins to gauge popularity
*
* @ORMOneToMany(targetEntity="Page",mappedBy="background")
*/
protected $pages;
}
class Page
{
/**
* @var string
*
* @ORMManyToOne(targetEntity="Background",inversedBy="pages")
* @ORMJoinColumn(name="background_id",referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
protected $background;
/**
* @ORMOneToMany(targetEntity="Background", mappedBy="page", cascade={"remove"})
* @ORMOrderBy({"id" = "ASC"})
*/
protected $customBackgrounds;
}
tryCCD_ 1并进行架构更新。
EDIT:问题出在数据库级别,而不是条令级别,cascade="remove"解决了这一问题,但是page_id的外键会保留,onDelete表示如果删除了具有该外键关系的列,则将字段设置为"value",这种情况下为null。如果您在模式更新之前转储sql,您将看到添加的查询,类似于"ON DELETE SET*"
更多信息可以在这里找到:http://www.techonthenet.com/oracle/foreign_keys/foreign_null.php
您唯一能做的就是在实体中使用事件侦听器,当页面被删除时,将其背景设置为null或其他id
/**
* @ORMOneToMany(targetEntity="Background", mappedBy="page", orphanRemoval=true)
* @ORMOrderBy({"id" = "ASC"})
*/
protected $customBackgrounds;
应该做到这一点(条令-孤儿移除)