2016-07-28 64 views
1

我在那裏,我不得不使用級聯= {「刪除」,「堅持」}因爲problem described here.如何從Symfony2中的實體中刪除cascade = {「remove」,「persist」}的用法?

通過documentation讀一個項目,引述:

即使自動級聯方便,應該小心使用。不要盲目地將級聯=全部應用於所有關聯,因爲它會不必要地降低應用程序的性能。對於被激活的每個級聯操作,Doctrine也將該操作應用於該關聯,無論是單個還是集合值。

而且我看到,同樣可以是固定的,如果我使用

$em->persist($entity); 

在我的堅持的服務,這我已經打電話。然而,學說不能按預期工作。這是我的實體。

實體/ Employee.php

<?php 
namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping AS ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(
*    name="UNIQ_EMPLOYEE_ID_NAME_ADDRESS_STATE_CITY_COUNTRY", 
*    columns={"id","name","city","state","country"} 
*  )}) 
*/ 
class Employee 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer", options={"unsigned":true}) 
    * @ORM\GeneratedValue(strategy="NONE") 
    */ 
    private $id; 

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

    /** 
    * @ORM\Column(type="text", nullable=false) 
    */ 
    private $address; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=false) 
    */ 
    private $city; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=false) 
    */ 
    private $state; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=false) 
    */ 
    private $country; 


    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Department", inversedBy="employee", cascade={"remove","persist"}) 
    * @ORM\JoinColumn(name="department_id", referencedColumnName="id", nullable=false) 
    */ 
    private $department; 

    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Transfer", mappedBy="employee", cascade={"remove","persist"}) 
    */ 
    private $transfer; 
} 

?> 

實體/ Department.php

<?php 
namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping AS ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\Common\Collections\Collection; 
use Doctrine\Common\Collections\Selectable; 

/** 
* @ORM\Entity 
* @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(
*    name="UNIQ_DEPARTMENT_ID_NAME", 
*    columns={"id","name"} 
*  )}) 
*/ 
class Department 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer", options={"unsigned":true}) 
    * @ORM\GeneratedValue(strategy="NONE") 
    */ 
    private $id; 

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

    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Employee", mappedBy="department", cascade={"remove","persist"}) 
    */ 
    private $employee; 

    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Transfer", mappedBy="department", cascade={"remove","persist"}) 
    */ 
    private $transfer; 
} 

?> 

實體/ Transfer.php

<?php 
namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping AS ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\Common\Collections\Collection; 
use Doctrine\Common\Collections\Selectable; 

/** 
* @ORM\Entity 
* @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(
*    name="UNIQ_TRANSFER_ID_DEPARTMENT_EMPLOYEE_START_END", 
*    columns={"id","name"} 
*  )}) 
*/ 
class Transfer 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer", options={"unsigned":true}) 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="date", length=255, nullable=false) 
    */ 
    private $start; 

    /** 
    * @ORM\Column(type="date", length=255, nullable=false) 
    */ 
    private $end; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee", inversedBy="attendance", cascade={"persist","remove"}) 
    * @ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false) 
    */ 
    private $employee; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Department", inversedBy="attendance", cascade={"persist","remove"}) 
    * @ORM\JoinColumn(name="department_id", referencedColumnName="id", nullable=false) 
    */ 
    private $department; 
} 

?> 

更新1:

現在,我有另一個問題。由於我的員工和部門的GeneratorValue策略是NONE,所以我有重複記錄錯誤的問題。我試圖使用PreFlushEventArgs在持久化之前刪除實體,如果記錄存在於數據庫中。但我想知道它是否應該那麼複雜?

感謝您的幫助提前。

回答

2

看起來你是從員工持續到部門和轉移的操作。但他們也將堅持運營級聯到員工實體。

這意味着,當你做

$em->persist($an_employee); 

你被困在一個持續循環。

在我看來,級聯堅持只應該以一種方式,即只在Employee實體上。

此外,如果你選擇這樣做,添加部的員工,而不是相反:

$an_employee->addDepartment($a_department); 
$an_employee->addTransfer($a_transfer); 

這樣,當你堅持下去的員工,其部門和轉讓也應該被堅持

+0

我已經更新了我的問題,請參閱..如果你能幫助我。我想我應該通過轉移模式進行更新,因爲員工可能已經存在。 – Gaurav

相關問題