【问题标题】:Symfony Doctrine2 Doesn't insert an available IDSymfony Doctrine2 不插入可用的 ID
【发布时间】:2014-06-18 22:19:55
【问题描述】:

我有一个关于 Doctrine2 和 Oracle 的新问题...

我将一个应用程序从 Symfony1 迁移到 symfony2。当我使用 Doctrine2 在生产数据库中插入一个新条目时,我收到错误“ORA-00001:违反唯一约束”。它尝试使用 ID 1 插入,如果我再试一次,它会尝试使用 ID 2 等插入...

这是我设置实体的方式:

    <?php

namespace EspaceApprenti\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ApprenticeMark
 *
 * @ORM\Table(name="APPRENTICE_MARK", indexes={@ORM\Index(name="IDX_8582BCF7105754FC", columns={"FK_BRANCH"}), @ORM\Index(name="IDX_8582BCF7C9387C17", columns={"FK_YEAR"}), @ORM\Index(name="IDX_8582BCF73B451C64", columns={"FK_MARKTYPE"})})
 * @ORM\Entity(repositoryClass="EspaceApprenti\UserBundle\Entity\ApprenticeMarkRepository")
 */
class ApprenticeMark
{
    /**
     * @var integer
     *
     * @ORM\Column(name="COEFFICIENT", type="bigint", nullable=false)
     */
    private $coefficient;

    /**
     * @var integer
     *
     * @ORM\Column(name="ID", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="RESULT", type="decimal", precision=2, scale=1, nullable=false)
     */
    private $result;

    /**
     * @var \ApprenticeBranch
     *
     * @ORM\ManyToOne(targetEntity="ApprenticeBranch")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="FK_BRANCH", referencedColumnName="ID")
     * })
     */
    private $fkBranch;

    /**
     * @var \ApprenticeYear
     *
     * @ORM\ManyToOne(targetEntity="ApprenticeYear")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="FK_YEAR", referencedColumnName="ID", onDelete="CASCADE")
     * })
     */
    private $fkYear;

    /**
     * @var \ApprenticeMarktype
     *
     * @ORM\ManyToOne(targetEntity="ApprenticeMarktype")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="FK_MARKTYPE", referencedColumnName="ID")
     * })
     */
    private $fkMarktype;



    /**
     * Set coefficient
     *
     * @param integer $coefficient
     * @return ApprenticeMark
     */
    public function setCoefficient($coefficient)
    {
        $this->coefficient = $coefficient;

        return $this;
    }

    /**
     * Get coefficient
     *
     * @return integer 
     */
    public function getCoefficient()
    {
        return $this->coefficient;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set result
     *
     * @param integer $result
     * @return ApprenticeMark
     */
    public function setResult($result)
    {
        $this->result = $result;

        return $this;
    }

    /**
     * Get result
     *
     * @return integer
     */
    public function getResult()
    {
        return $this->result;
    }

    /**
     * Set fkBranch
     *
     * @param \EspaceApprenti\UserBundle\Entity\ApprenticeBranch $fkBranch
     * @return ApprenticeMark
     */
    public function setFkBranch(\EspaceApprenti\UserBundle\Entity\ApprenticeBranch $fkBranch = null)
    {
        $this->fkBranch = $fkBranch;

        return $this;
    }

    /**
     * Get fkBranch
     *
     * @return \EspaceApprenti\UserBundle\Entity\ApprenticeBranch 
     */
    public function getFkBranch()
    {
        return $this->fkBranch;
    }

    /**
     * Set fkYear
     *
     * @param \EspaceApprenti\UserBundle\Entity\ApprenticeYear $fkYear
     * @return ApprenticeMark
     */
    public function setFkYear(\EspaceApprenti\UserBundle\Entity\ApprenticeYear $fkYear = null)
    {
        $this->fkYear = $fkYear;

        return $this;
    }

    /**
     * Get fkYear
     *
     * @return \EspaceApprenti\UserBundle\Entity\ApprenticeYear 
     */
    public function getFkYear()
    {
        return $this->fkYear;
    }

    /**
     * Set fkMarktype
     *
     * @param \EspaceApprenti\UserBundle\Entity\ApprenticeMarktype $fkMarktype
     * @return ApprenticeMark
     */
    public function setFkMarktype(\EspaceApprenti\UserBundle\Entity\ApprenticeMarktype $fkMarktype = null)
    {
        $this->fkMarktype = $fkMarktype;

        return $this;
    }

    /**
     * Get fkMarktype
     *
     * @return \EspaceApprenti\UserBundle\Entity\ApprenticeMarktype 
     */
    public function getFkMarktype()
    {
        return $this->fkMarktype;
    }
}

这是控制器的代码:

public function newAction($idYear,$idYeartype)
    {
        $m = $this->getDoctrine()
                  ->getManager();

        // Get year
        $year = $m->getRepository('EspaceApprentiUserBundle:ApprenticeYear')->find($idYear);
        if (!$year) {
                throw $this->createNotFoundException('Year not found');
        }
        // Get yeartype
        $yeartype = $m->getRepository('EspaceApprentiUserBundle:ApprenticeYeartype')->find($idYeartype);
        if (!$yeartype) {
                throw $this->createNotFoundException('Year type not found');
        }
        // Get apprentice
        $apprentice = $m->getRepository('EspaceApprentiUserBundle:ApprenticeApprentice')->find($year->getFkApprentice()->getId());
        if (!$apprentice) {
                throw $this->createNotFoundException('Apprentice type not found');
        }

        $mark = new ApprenticeMark();
        $mark->setFkYear($year);
        $form = $this->createForm(new ApprenticeMarkType($yeartype), $mark);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
                $form->bind($request);
                if ($form->isValid()) {
                        $m->persist($mark);
                        $m->flush();
                        return $this->redirect($this->generateUrl('grids_apprentice_index',array('idApprentice' => $apprentice->getId())));
                }
        }

        return $this->render('EspaceApprentiGridsBundle:Grids_Mark:new.html.twig', array('apprentice' => $apprentice, 'form' => $form->createView()));
    }

如何配置 Doctrine2 以获取最后一个 ID 而不仅仅是从 1 递增? 还是我应该手动获取并插入最后一个 ID?

问候

【问题讨论】:

  • 你更新你的架构了吗?
  • 是的,我做到了:“php app/console 学说:schema:update --force”
  • 我在尝试验证架构时也出现以下错误:[映射] 正常 - 映射文件正确。 [Database] FAIL - 数据库架构与当前映射文件不同步。即使我在stackoverflow.com/questions/13670471/… 之前更新 --dump-sql
  • 模式更新力的输出是什么?
  • 更新数据库架构... 数据库架构更新成功!执行了“8”个查询

标签: php sql oracle symfony doctrine-orm


【解决方案1】:

我找到了解决问题的方法。显然我在 Oracle 中的 SEQUENCES 不是最新的,所以我不得不手动更新它们。

【讨论】:

    猜你喜欢
    • 2011-11-02
    • 2014-09-09
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多