【问题标题】:Doctrine insert into Oracle 12c DB table with auto generated id使用自动生成的 ID 将教义插入 Oracle 12c 数据库表
【发布时间】:2019-03-16 13:34:49
【问题描述】:

我试图在我的数据库 (Oracle 12c) 表中插入一个新条目,但我没有这样做

以下是我的实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Divisions
 *
 * @ORM\Table(name="DIVISIONS")
 * @ORM\Entity
 */
class Divisions
{

    /**
     * @var int
     *
     * @ORM\Column(name="DIVISIONID", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="DIVISIONS_DIVISIONID_seq", allocationSize=1, initialValue=1)
     */
    public $divisionid = '"SPECIFICATIONS"."ISEQ$$_79111".nextval';

    /**
     * @var string|null
     *
     * @ORM\Column(name="DIVISIONNAME", type="string", length=500, nullable=true)
     */
    public $divisionname;

    /**
     * @var int|null
     *
     * @ORM\Column(name="SORTORDER", type="integer", nullable=true, options={"default"="1"})
     */
    public $sortorder = '1';

    /**
     * @var int|null
     *
     * @ORM\Column(name="ISDELETED", type="integer", nullable=true)
     */
    public $isdeleted = '0';

    public function getDivisionid(): ?int
    {
        return $this->divisionid;
    }

    public function getDivisionname(): ?string
    {
        return $this->divisionname;
    }

    public function setDivisionname(?string $divisionname): self
    {
        $this->divisionname = $divisionname;

        return $this;
    }

    public function getSortorder(): ?int
    {
        return $this->sortorder;
    }

    public function setSortorder(?int $sortorder): self
    {
        $this->sortorder = $sortorder;

        return $this;
    }

    public function getIsdeleted(): ?int
    {
        return $this->isdeleted;
    }

    public function setIsdeleted(?int $isdeleted): self
    {
        $this->isdeleted = $isdeleted;

        return $this;
    }


}

这是我的控制器,它正在尝试“发布”并添加一个新部门

<?php

namespace App\Controller;

use App\Entity\Divisions;
use App\Form\DivisionsType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * @Route("api/divisions")
 */
class DivisionsController extends AbstractController
{

    /**
     * @Route("", name="divisions_add", methods={"POST"})
     */
    public function addDivisions(Request $request)
    {
      $em = $this->getDoctrine()->getManager();
      $division = new Divisions();
      $division->setDivisionname('TestDiv');
      $em->persist($division);
      $em->flush();
      return new Response(
        Response::HTTP_OK
      );
    }


}

当我尝试调用它时,会出现以下错误消息:

An exception occurred while executing 'INSERT INTO DIVISIONS (DIVISIONID, DIVISIONNAME, SORTORDER, ISDELETED) VALUES (?, ?, ?, ?)' with params [16, "TestDiv", "1", "0"]:

ORA-32795: cannot insert into a generated always identity column

出于某种原因,无论我尝试什么,都会调用 DivisionID 列。有没有办法在不调用某些特定列的情况下插入?

或者有没有办法将其作为“INSERT INTO DIVISIONS (DIVISIONNAME, SORTORDER, ISDELETED) VALUES (?, ?, ?)' 发送,参数为 ["TestDiv", "1", "0"]'

PS:实体是从数据库中自动生成的

如果有人想要更多信息,我很乐意提供

【问题讨论】:

    标签: php oracle symfony doctrine dql


    【解决方案1】:

    好的,如果有人达到这一点并且仍然被卡住,我会找到一种解决方法:

    我将我的实体更改为如下所示:

    <?php
    
    namespace App\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * Divisions
     *
     * @ORM\Table(name="DIVISIONS")
     * @ORM\Entity
     */
    class Divisions
    {
        /**
         * @var int
         *
         * @ORM\Column(name="DIVISIONID", type="integer", nullable=false)
         * @ORM\Id
         */
        public $divisionid;
    
        /**
         * @var string|null
         *
         * @ORM\Column(name="DIVISIONNAME", type="string", length=500, nullable=true)
         */
        public $divisionname;
    
        /**
         * @var int|null
         *
         * @ORM\Column(name="SORTORDER", type="integer", nullable=true, options={"default"="1"})
         */
        public $sortorder = '1';
    
        /**
         * @var int|null
         *
         * @ORM\Column(name="ISDELETED", type="integer", nullable=true)
         */
        public $isdeleted = '0';
    
        public function getDivisionid(): ?int
        {
            return $this->divisionid;
        }
    
        public function getDivisionname(): ?string
        {
            return $this->divisionname;
        }
    
        public function setDivisionname(?string $divisionname): self
        {
            $this->divisionname = $divisionname;
    
            return $this;
        }
    
        public function getSortorder(): ?int
        {
            return $this->sortorder;
        }
    
        public function setSortorder(?int $sortorder): self
        {
            $this->sortorder = $sortorder;
    
            return $this;
        }
    
        public function getIsdeleted(): ?int
        {
            return $this->isdeleted;
        }
    
        public function setIsdeleted(?int $isdeleted): self
        {
            $this->isdeleted = $isdeleted;
    
            return $this;
        }
    
    
    }
    

    然后在Controller中我添加了以下功能:

    private function getMaxDivisionIdNumber() {
      $em = $this->getDoctrine()->getManager();
      $query = $em->createQuery('SELECT MAX(u.divisionid) FROM App\Entity\Divisions u');
      $res = $query->getResult();
      return $res[0][1];
    }
    

    现在这个函数用于获取 Max id 号.. 所以我的控制器看起来像这样:

    <?php
    
    namespace App\Controller;
    
    use App\Entity\Divisions;
    use App\Form\DivisionsType;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    
    /**
     * @Route("api/divisions")
     */
    class DivisionsController extends AbstractController
    {
    
        /**
         * @Route("", name="divisions_add", methods={"POST"})
         */
        public function addDivisions(Request $request)
        {
          $em = $this->getDoctrine()->getManager();
          $content = $request->getContent();
          $content = json_decode($content);
          $division = new Divisions();
          foreach ($content as $key => $value) {
            $division->$key = $value; 
          }
          $maxId = (int) $this->getMaxDivisionIdNumber();
          $division->divisionid = $maxId + 1;
          $em->persist($division);
          $em->flush();
          return new Response(
            Response::HTTP_OK
          );
        }
    
       private function getMaxDivisionIdNumber() {
        $em = $this->getDoctrine()->getManager();
        $query = $em->createQuery('SELECT MAX(u.divisionid) FROM App\Entity\Divisions u');
        $res = $query->getResult();
        return $res[0][1];
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多