学说坚持和flush会导致Symfony中的重复记录

时间:2016-06-28 14:18:48

标签: php mysql symfony doctrine-orm

当我打电话给坚持&在PersonalDevelopmentRequest实体上刷新它会导致DB中存在重复记录。

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

更新:当我在控制器末尾删除重定向时,Doctrine仅保留记录一次。重定向可以与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;

  ...

有谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:0)

使用给定的代码我只能猜测持久存储到数据库的代码块不在条件块内(不在if条件中),并且在您的更新中,您说如果删除redirectToRoute它解决它。所以它可能是一个重定向循环,你可能会去同一个控制器两次并持续两次,我建议把它放在一个if条件,如$form->isValid()条件,以避免自由持久化记录

答案 1 :(得分:0)

我是因为在$kernel->handle($request);中重复app.php而造成的。