2016-06-28 124 views
0

當我呼籲持續&刷新PersonalDevelopmentRequest實體時,它會導致數據庫中存在重複記錄。主義堅持和沖洗在Symfony中導致重複記錄

我使用PHP 7.0.7,MySQL 5.6.28和Symfony 2.8.7。

UPDATE:當我在控制器的末尾刪除重定向時,Doctrine只保留一次記錄。可以重定向與教義有關嗎?

守則控制器:

$request = new PersonalDevelopmentRequest(); 
$request 
    ->setTrainingGroup($training->getTrainingGroup()) 
    ->setTraining($training) 
    ->setEmployee($this->getUser()) 
    ->setName($training->getName()); 

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

$em->persist($request); 
$em->flush(); 

$this->addFlash('success', 'Mám to.'); 

return $this->redirectToRoute('personal_development'); 

實體:

/** 
* PersonalDevelopmentRequest. 
* 
* @ORM\Table(name="personal_development_request") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\PersonalDevelopmentRequestRepository") 
* @Gedmo\SoftDeleteable() 
*/ 
class PersonalDevelopmentRequest implements WorkflowInterface 
{ 
    use BlameableEntity; 
    use SoftDeleteableEntity; 
    use TimestampableEntity; 

    /** 
    * @ORM\Column(name="id", type="guid") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="UUID") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="date", nullable=true) 
    */ 
    private $period; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="personalDevelopmentRequests") 
    */ 
    private $employee; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User") 
    * @ORM\JoinColumn(nullable=true) 
    */ 
    private $employeeForRelation; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\TrainingGroup", inversedBy="requests") 
    * @ORM\JoinColumn(nullable=false) 
    */ 
    private $trainingGroup; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Training", inversedBy="requests") 
    * @ORM\JoinColumn(nullable=true) 
    */ 
    private $training; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\AnnualReview", inversedBy="requests") 
    */ 
    private $annualReview; 

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

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

    const TYPE_ANNUAL_REVIEW = 'annual_review'; 
    const TYPE_MANUAL = 'manual'; 
    const TYPE_TRAINING_PLAN = 'training_plan'; 

    /** 
    * @ORM\Column(type="string", length=16) 
    */ 
    private $type; 

    const STATE_SENT = 'sent'; 
    const STATE_DECLINED = 'declined'; 
    const STATE_APPROVED = 'approved'; 
    const STATE_FINISHED = 'finished'; 
    const STATE_UNFINISHED = 'unfinished'; 

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

    const RESULT_DIDNT_COMPLETE = 0; 
    const RESULT_COMPLETED_PASSED = 1; 
    const RESULT_COMPLETED_FAILED = 2; 

    /** 
    * @ORM\Column(type="integer", nullable=true) 
    */ 
    private $result; 

    /** 
    * @ORM\Column(type="float", nullable=true) 
    */ 
    private $cost; 

    /** 
    * @ORM\Column(type="date", nullable=true) 
    */ 
    private $mustBeRenewedAfter; 

    const TRAINING_TYPE_NONE = null; 
    const TRAINING_TYPE_INT = 'int'; 
    const TRAINING_TYPE_EXT = 'ext'; 

    /** 
    * @ORM\Column(type="string", nullable=true) 
    */ 
    private $trainingType; 

    /** 
    * @ORM\Column(type="integer", nullable=true) 
    */ 
    private $rangeInHours; 

    /** 
    * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Log") 
    * @ORM\JoinTable(name="personal_development_request_logs", 
    * joinColumns={@ORM\JoinColumn(name="personal_development_request_id", referencedColumnName="id")}, 
    * inverseJoinColumns={@ORM\JoinColumn(name="log_id", referencedColumnName="id", unique=true)} 
    *) 
    */ 
    private $logs; 

    ... 

誰能告訴我什麼,我做錯了什麼?

+0

你能告訴我們你的控制器的名稱和路線嗎? – Kapil

回答

0

我是由於在app.php中複製了$kernel->handle($request);而引起。

0

與給定的代碼,我只能猜測你的代碼塊,堅持到數據庫不在一個條件塊(不在一個條件),並與你的更新,你說如果你刪除redirectToRoute它解決它。所以它可能是一個重定向的循環,你可能會去同一個控制器兩次,並堅持它兩次,我會建議把它放在如條件,如$form->isValid()條件,例如,以避免自由堅持記錄

+0

我貼出整個動作,沒有任何形式。它真的好像redirectToRoute由於某種原因導致它。 – fmstoun