注释中的不同2 /教义2 [英] Different in Annotation Symfony 2 / Doctrine 2

查看:79
本文介绍了注释中的不同2 /教义2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两者有什么区别?

  / ** 
* @ ORM\ManyToOne(targetEntity =Category,inversedBy =products,cascade = {remove})
* @ ORM\JoinColumn(name =category_id,referencedColumnName =id)
* /
protected $ category;

  / ** 
*
* @ ORM\ManyToOne(targetEntity =Category,inversedBy =products)
* @ ORM\JoinColumn(name =category_id ,referencedColumnName =id,onDelete =CASCADE))
* /
protected $ category;

谢谢!

解决方案

第一个告诉ORM执行级联。因此,Doctrine会将数据保留在内存中以执行删除级联。



第二个将告诉数据库执行onDelete级联,从doctrine卸载进程。 p>

请记住,第一个选项将保留内存中的数组/关联来执行删除级联,这可能非常重。



第一个参数的典型用例是数据模型图不是太重的情况,您真的希望对每个实体生命周期事件(即使是使用级联删除的那些事件)也是有利的。



第二个不足之处在于,您无法对这些实体生命周期事件进行尝试,但如果您的数据模型/图表太重,则这可以是处理级联的唯一选项。 / p>

What is the difference between these two?

/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products",cascade={"remove"})
* @ORM\JoinColumn(name="category_id", referencedColumnName="id") 
*/
protected $category;

and

/**
 *
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE"))
 */
protected $category;

Thanks !

解决方案

The first tells the ORM to perform itself the cascade. Hence Doctrine will keep data in memory to perform itself the delete cascade.

The second will tell to the database to perform the onDelete cascade, unloading the process from doctrine.

Remember that the first option will keep in memory the arrays / associations to perform the delete cascade, which can be really heavy.

Typical use case for the first parameter is a case where your data model graph isn't too heavy, and you really want to benfit every entity lifecycle events, even those who are removed using the cascade.

The downside of the second is that you can't benfit these entity lifecycle events, but if your data model / graph is really too heavy, this can be the only option to deal with cascade.

这篇关于注释中的不同2 /教义2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆