使用自动生成的 ID 将 Doctrine 插入 Oracle 12c DB table
Doctrine insert into Oracle 12c DB table with auto generated id
我正在尝试向我的数据库 (Oracle 12c) 中插入一个新条目 table,但我没有这样做
以下是我的实体:
<?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 列都会被调用。有没有办法在不调用某些特定列的情况下插入?
或者有没有办法将其作为 'INSERT INTO DIVISIONS (DIVISIONNAME, SORTORDER, ISDELETED) VALUES (?, ?, ?)' 发送,并带有参数 ["TestDiv", "1", "0"]'
PS: 实体是从数据库自动生成的
如果有人需要更多信息,我很乐意提供
好的,所以如果有人达到这一点但仍然卡住,我会找到一种解决方法:
我将实体更改为如下所示:
<?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];
}
}
我正在尝试向我的数据库 (Oracle 12c) 中插入一个新条目 table,但我没有这样做
以下是我的实体:
<?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 列都会被调用。有没有办法在不调用某些特定列的情况下插入?
或者有没有办法将其作为 'INSERT INTO DIVISIONS (DIVISIONNAME, SORTORDER, ISDELETED) VALUES (?, ?, ?)' 发送,并带有参数 ["TestDiv", "1", "0"]'
PS: 实体是从数据库自动生成的
如果有人需要更多信息,我很乐意提供
好的,所以如果有人达到这一点但仍然卡住,我会找到一种解决方法:
我将实体更改为如下所示:
<?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];
}
}