我在'父'映射的实体(用户)上有一个自引用的一对多关联,并由'children'反转.我希望能够删除不是父母的用户.我的实体声明如下.
class User implements UserInterface
{
/**
* @ORM\Column(name="id", type="smallint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
(...)
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="parent")
*/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
public function __construct()
{
$this->parentId = null; // Default value for column parent_id
$this->children = new ArrayCollection();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试删除不是父项的子用户时,我收到以下错误.
带有消息'SQLSTATE [23000]的异常'PDOException':完整性约束违规:1451无法删除或更新父行:外键约束失败
我尝试过级联删除,如下所示.
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="parent", cascade={"persist", "remove"})
*/
protected $children;
Run Code Online (Sandbox Code Playgroud)
无论如何都会得到同样的错误.有帮助吗?