本文介绍了学说 2 级联 ={''remove"} 似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程

namespace MPUserRegistrationBundleEntity;

use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonPersistencePersistentObject;
use MPServicesSiteAdapterBundleUtilString;
/**
 * @ORMTable(name="customer")
 * @ORMEntity(repositoryClass="MPUserRegistrationBundleRepositoriesCustomerRepository")
 * @ORMHasLifecycleCallbacks
 */
class Customer extends PersistentObject
{

    /**
     * @var string $id
     * @ORMId
     * @ORMColumn(name="icustomer_id", type="integer")
     */
    protected $id;

    /**
     * @var string $addresses
     * @ORMOneToMany(targetEntity="MPUserRegistrationBundleEntityAddress", mappedBy="customer", cascade={"remove"})
     */
    protected $addresses;

有如下关系

/**
 * MPUserRegistrationBundleEntity
 */
namespace MPUserRegistrationBundleEntity;

use DoctrineORMMapping as ORM;
use DoctrineCommonPersistencePersistentObject;

/**
 * @ORMTable(name="custdeladd")
 * @ORMEntity(repositoryClass="MPUserRegistrationBundleRepositoriesAddressRepository")
 */
class Address extends PersistentObject
{
      /**
       * @var integer $suffix
       * @ORMColumn(name="isuffix", type="integer")
       * @ORMId
       */
      protected $suffix;

      /**
       * @var object $customer
       * @ORMManyToOne(targetEntity="MPUserRegistrationBundleEntityCustomer", inversedBy="addresses", cascade={"persist"})
       * @ORMJoinColumn(name="icustomer_id", referencedColumnName="icustomer_id")
       */
      protected $customer;
}

有谁知道为什么当客户被删除时地址没有?非常感谢

Does anybody know why when the customer gets deleted the addresses aren't? Many thanks

推荐答案

您的关系定义似乎没问题.删除客户的方式是什么?我的意思是 Doctrine 不会直接在数据库中设置ON DELETE CASCADE".因此,如果您以教义"以外的其他方式删除客户实体,评论将不会被删除.

Your relationship definition seems to be fine. What is the way the customer is deleted? I mean Doctrine doesn't set "ON DELETE CASCADE" directly in database. So, if you remove customer entity in other way than "doctrine's" one, comments won't be deleted.

您可以通过添加注释来告诉教义直接在数据库中设置它:

You may tell doctrine to set this directly in database, by adding in annotation:

@ORMJoinColumn(name="icustomer_id", referencedColumnName="icustomer_id", onDelete="CASCADE")

但是,如果您尝试以正确的方式删除实体 ant 这仍然不起作用,请尝试将orphanRemoval"添加到 true,它应该会有所帮助:

But if you're trying remove the entity in right-doctrine way ant this still doesn't work, try add "orphanRemoval" to true, it should help:

// Customer.php
/**
 * @var string $addresses
 * @ORMOneToMany(targetEntity="MPUserRegistrationBundleEntityAddress", mappedBy="customer", cascade={"remove"}, orphanRemoval=true)
 */
protected $addresses;

这篇关于学说 2 级联 ={''remove"} 似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:14