dongxi1320 2014-05-02 09:13
浏览 48
已采纳

Symfony Doctrine2不插入可用的ID

I have a new problem related to Doctrine2 and Oracle...

I migrated an application from Symfony1 to symfony2. When I insert a new entry in the production database with Doctrine2, I get the error "ORA-00001: unique constraint violated". It tries to insert with the ID 1, if I try again it tries to insert with ID 2 etc...

Here is how I setup my entity :

    <?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;
    }
}

Here is the code of the controller :

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()));
    }

How do I configure Doctrine2 to get the last ID and not just increment from 1 ? Or should I get and insert the last ID manually ?

Regards

  • 写回答

1条回答 默认 最新

  • dongyu3967 2014-05-05 14:32
    关注

    I found the solution to my problem. Apparently my SEQUENCES in Oracle were not up to date, so I had to manually update them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP