Symfony Doctrine2不会插入可用的ID

时间:2014-05-02 09:13:24

标签: php sql oracle symfony doctrine-orm

我有一个与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吗?

此致

1 个答案:

答案 0 :(得分:1)

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