ORM主义ManyToOne更新CASCADE(Symfony) [英] ORM Doctrine ManyToOne on update CASCADE (Symfony)
问题描述
我有两个实体
class Promotor
{
/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor")
* @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id", nullable=false)
*/
protected $ciudad;
和
class Ciudad
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=50)
*/
private $nombre;
促销者可以住在一个城市(Ciudad)。在Ciudad(城市)可以生活许多Promotores。
A "Promotor" can live in one "Ciudad" (City). And in a "Ciudad" (City) can live many "Promotores".
如果我在JoinColumn中添加onDelete =CASCADE
If i add onDelete="CASCADE" in JoinColumn
/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor")
* @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
protected $ciudad;
它生成下一个代码
ALTER TABLE promotor DROP FOREIGN KEY FK_BF20A37FE8608214;
ALTER TABLE promotor ADD CONSTRAINT FK_BF20A37FE8608214 FOREIGN KEY (ciudad_id)
REFERENCES Ciudad (id) ON DELETE CASCADE
,但我也喜欢做CASCADE更新。我尝试使用onUpdate =CASCADE,但它不工作
but also i like do CASCADE on update. I try with onUpdate="CASCADE" but it doesn' work
[Doctrine\Common\Annotations\AnnotationException]
[Creation Error] The annotation @ORM\JoinColumn declared on property Web\PromotorBundle\Entity\Promotor::$ciudad does not have a property named
"onUpdate". Available properties: name, referencedColumnName, unique, nulla
ble, onDelete, columnDefinition, fieldName
通过错误我知道属性onUpdate不存在,但是..有更新吗?有没有办法做级联更新?
By the error I understand that the property onUpdate does not exist, but.. Is there any way to do cascade on update?
推荐答案
onDelete =CASCADE用于数据库级别。正如你已经说过没有onUpdate。另一个缺点是ON DELETE CASCADE仅适用于InnoDB。它不适用于MyISAM。
The onDelete="CASCADE" is used on the database level. As you already said there is no onUpdate. Another downside is that ON DELETE CASCADE only works on InnoDB. It doesn't work on MyISAM.
但是您可以使用Doctrine内存级联操作:
But you can use Doctrine in-memory cascade operations:
class Promotor
{
/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id", nullable=false)
*/
protected $ciudad;
It's all described in the documentation: http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations
加你可以跳过 JoinColumn 注释,因为你写的方式,是默认配置,它是隐式生成的。
Plus you can skip the JoinColumn annotation, because the way you have it written, is the default configuration and it's generated implicitly.
所以你可以写:
class Promotor
{
/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor", cascade={"persist", "remove"})
*/
protected $ciudad;
这篇关于ORM主义ManyToOne更新CASCADE(Symfony)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!