symfony2 deleteForm无法正常工作 [英] symfony2 deleteForm not working fine

查看:92
本文介绍了symfony2 deleteForm无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体.我想删除服务行.

I have 2 entities. I would like to delete row of Service.

我尝试过:

$ em = $ this-> getDoctrine()-> getEntityManager();

$em = $this->getDoctrine()->getEntityManager();

$ service = $ em-> getRepository('AppBundle:Service')-> find(1);

$service = $em->getRepository('AppBundle:Service')->find(1);

$ em->删除($ service);

$em->remove($service);

$ em-> flush();

$em->flush();

通过这种方式,它删除了包含该类别ID的所有行.对不起,我的英语不好.让我用另一种方式向您解释:

On this way it deleted all of the row, which containing this category id. Sry for my bad English. Let me explain you on the other way:

           BEFORE                     AFTER delete id 1 from service
+----+------+  +----+------+-----+  +----+------+  +----+------+-----+
+ id + ca_  +  + id + se_  + ca_ +  + id + ca_  +  + id + se_  + ca_ +
+    + name +  +    + name + id  +  +    + name +  +    + name + id  +
+----+------+  +----+------+-----+  +----+------+  +----+------+-----+
+ 1  + A    +  + 1  + A1   + 1   +  +    +      +  +    +      +     +
+----+------+  +----+------+-----+  +----+------+  +----+------+-----+
+ 2  + B    +  + 2  + A2   + 1   +  + 2  + B    +  +    +      +     +
+----+------+  +----+------+-----+  +----+------+  +----+------+-----+
+ 3  + C    +  + 3  + B1   + 2   +  + 3  + C    +  + 3  + B1   + 2   +
+----+------+  +----+------+-----+  +----+------+  +----+------+-----+

我该如何解决?

服务实体:

/**
* Service
*
* @ORM\Table(name="service")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ServiceRepository")
*/
class Service {
 ...

 /**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="services", cascade={"persist","remove"})
 * @ORM\JoinColumn(onDelete="CASCADE")
 */
 private $caId;

 /**
 * Set caId
 *
 * @param \AppBundle\Entity\Category $caId
 *
 * @return Service
 */
 public function setCaId(\AppBundle\Entity\Category $caId = null)
 {
     $this->caId = $caId;
     return $this;
 }

 /**
 * Get caId
 *
 * @return \AppBundle\Entity\Category
 */
 public function getCaId()
 {
     return $this->caId;
 }
}

类别实体:

/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category {
 ...

 /**
 * @ORM\OneToMany(
 *   targetEntity="Service",
 *   mappedBy="caId"
 * )
 */
 private $services;

 /**
 * Constructor
 */
 public function __construct()
 {
   $this->services = new ArrayCollection();
 }

 /**
 * Get orders
 *
 * @return Collection
 */
 public function getServices()
 {
   return $this->services;
 }
}


推荐答案

我看到了如何配置映射的两个问题:

I see two issues with how your mapping is configured:

  1. 在您的Service实体中,您已配置为级联删除操作.这意味着,当删除Service对象时,与之关联的Category实体也将被删除.

  1. In your Service entity, you have configured to cascade removal operations. This means that when a Service object is deleted, the Category entity it was associated with will be deleted too.

映射中的第二个关键部分是@ORM\JoinColumn(onDelete="CASCADE").在数据库级别上,这具有以下效果:当删除category表中的条目(由于1发生)时,service表中所有引用已删除类别的记录都将被删除(这就是为什么)第二项服务丢失).

The second crucial part in your mapping is the @ORM\JoinColumn(onDelete="CASCADE"). On the database level this has the effect that when an entry from the category table is removed (which happens because of 1), every record in the service table that refers to the deleted category, will be deleted to (that's why the second service gets lost).

通过查看模型,我认为将Service实体中的cascade={"persist", "remove"}更改为仅cascade={"persist"}就足够了(也许您甚至不需要级联持久性操作).

From looking at your model I assume that changing cascade={"persist", "remove"} in your Service entity to only cascade={"persist"} will be enough (maybe you even do not need to cascade persist operations).

这篇关于symfony2 deleteForm无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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