php - 教义持久化和刷新导致 Symfony 中的重复记录

标签 php mysql symfony doctrine-orm

当我对 PersonalDevelopmentRequest 实体调用 persist &lush 时,会导致数据库中出现重复记录。

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

更新:当我删除 Controller 末尾的重定向时,Doctrine 仅保留一次记录。可以重定向与 Doctrine 的关系吗?

Controller 中的代码:

$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;

  ...

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

最佳答案

使用给定的代码,我只能猜测持久保存到数据库的代码块不在条件 block 内(不在 if 条件内),并且根据您的更新,您说如果删除 redirectToRoute它解决了它。所以这可能是一个重定向循环,您可能会两次访问同一个 Controller 并持久化它两次,我建议将其放入 if 条件中,例如 $form->isValid() 条件避免随意保留记录的示例

关于php - 教义持久化和刷新导致 Symfony 中的重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38078758/

相关文章:

symfony - Symfony2+Twig 中的验证(vs)清理?

html - 在 {% spaceless %} 标签内的 block 内禁用 spaceless

javascript - angularjs 使用 $http.post 到 PHP 但得到空数组

php - 使用php从mysql中删除重复项

php - 带有文件下载的提交按钮

php - Facebook 注册

php - 部署 WAMP -> 实时站点 - 任何随机提示?

java - 错误本质上是致命的......对于进程还是线程?

mysql - 根据最大日期和客户 ID 获取数据

symfony - 如何构建 Symfony 2 应用程序以支持多个应用程序?