php ORM Doctrine ManyToOne on update CASCADE (Symfony)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16200990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:42:50  来源:igfitidea点击:

ORM Doctrine ManyToOne on update CASCADE (Symfony)

phpsymfonyormdoctrine-orm

提问by JGrinon

I have two entities

我有两个实体

class Promotor
{

/**
 * @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor")
 * @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id", nullable=false)
 */
protected $ciudad;

and

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;

A "Promotor" can live in one "Ciudad" (City). And in a "Ciudad" (City) can live many "Promotores".

一个“发起人”可以住在一个“Ciudad”(城市)中。而在一个“Ciudad”(城市)中可以住很多“Promotores”。

If i add onDelete="CASCADE" in JoinColumn

如果我在 JoinColumn 中添加 onDelete="CASCADE"

/**
 * @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor")
 * @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
 */
protected $ciudad;

it generate the next code

它生成下一个代码

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

but also i like do CASCADE on update. I try with onUpdate="CASCADE" but it doesn' work

但我也喜欢在更新时做级联。我尝试使用 onUpdate="CASCADE" 但它不起作用

[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

By the error I understand that the property onUpdate does not exist, but.. Is there any way to do cascade on update?

通过错误,我知道属性 onUpdate 不存在,但是.. 有没有办法在更新时进行级联?

回答by alsar

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.

onDelete="CASCADE" 用于数据库级别。正如您已经说过的,没有 onUpdate。另一个缺点是 ON DELETE CASCADE 仅适用于 InnoDB。它不适用于 MyISAM。

But you can use Doctrine in-memory cascade operations:

但是您可以使用 Doctrine 内存级联操作:

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

这一切都在文档中进行了描述:http: //docs.doctrine-project.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations

Plus you can skip the JoinColumnannotation, because the way you have it written, is the default configuration and it's generated implicitly.

另外,您可以跳过JoinColumn注释,因为您编写它的方式是默认配置,并且它是隐式生成的。

So you can just write:

所以你可以写:

class Promotor
{

/**
* @ORM\ManyToOne(targetEntity="Ciudad", inversedBy="promotor", cascade={"persist", "remove"})
*/
protected $ciudad;