Doctrine2删除不需要的级联

时间:2013-11-07 09:43:27

标签: doctrine-orm cascade

我正在开发产品目录,并且有两个实体,PcatSalesItem和PcatCategory,它们之间具有多对多关系。如果我删除一个类别,并且仍然有与之关联的销售项目,我希望抛出异常,我不希望级联删除。在RDBMS级别(PostgreSQL)上,在连接表中,我将外键设置为“ON DELETE RESTRICT”。但是,当我删除具有销售项目的类别时,Doctrine会执行级联删除。我没有指定cascade = remove to Doctrine!

以下是实体:

/**
 * PcatSalesItem
 *
 * @ORM\Table(name="pcat_sales_item")
 * @ORM\Entity
 * @Gedmo\Loggable(logEntryClass="Qi\Bss\BaseBundle\Entity\Business\LogEntryBusiness")
 */
class PcatSalesItem
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="pcat_sales_item_id_seq", allocationSize=1, initialValue=1)
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=64, nullable=false)
     * @Gedmo\Versioned
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=true)
     * @Gedmo\Versioned
     */
    private $description;

    /**
     * @var array $categories
     *
     * @ORM\ManyToMany(targetEntity="PcatCategory")
     * @ORM\JoinTable(name="pcat_category_x_sales_item",
     *      joinColumns={@ORM\JoinColumn(name="sales_item_id", referencedColumnName="id", onDelete="RESTRICT")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="RESTRICT")}
     *      )
     */
    private $categories;
    ...
}

/**
 * PcatCategory
 *
 * @ORM\Table(name="pcat_category")
 * @ORM\Entity
 * @Gedmo\Loggable(logEntryClass="Qi\Bss\BaseBundle\Entity\Business\LogEntryBusiness")
 */
class PcatCategory
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="pcat_category_id_seq", allocationSize=1, initialValue=1)
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=64, nullable=false)
     * @Gedmo\Versioned
     */
    private $name;

    /**
     * @var array $salesItems
     *
     * @ORM\ManyToMany(targetEntity="PcatSalesItem")
     * @ORM\JoinTable(name="pcat_category_x_sales_item",
     *      joinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="RESTRICT")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="sales_item_id", referencedColumnName="id", onDelete="RESTRICT")}
     *      )
     */
    private $salesItems;
    ....
}

以下是我用来删除类别的代码:

$em = $this->getDoctrine()->getManager();
$cat = $em->getRepository('QiBssBaseBundle:PcatCategory')->find(15);
$em->remove($cat);
$em->flush();

任何帮助将不胜感激!

0 个答案:

没有答案