使用自动生成的ID将Doctrine插入Oracle 12c DB表

时间:2018-10-11 12:52:54

标签: php oracle symfony doctrine dql

我正在尝试在数据库(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;
    }


}

这是我的控制器,试图“ POST”并添加新的部门

<?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列。.是否可以在不调用某些特定列的情况下进行插入?

或者有一种方法可以使用参数[“ TestDiv”,“ 1”,“ 0”]来将其作为“插入分区(DIVISIONNAME,SORTORDER,ISDELETED)值(?,?,?)”发送>

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

如果有人想得到更多不愉快的信息

1 个答案:

答案 0 :(得分:0)

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

我将实体更改为以下样子:

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


}

然后在控制器中我添加了以下功能:

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

现在此函数用于获取最大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];
   }

}