Im trying to insert into my database (Oracle 12c) table a new entry but im failing to do that
The following is my entity:
<?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;
}
}
And here is my controller that is trying to "POST" and add a new Division
<?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
);
}
}
when i try to call this the following Error message will appear:
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
For some reason no matter what i try the DivisionID column will be called.. is there a way to insert without calling some certain columns?
Or is there a way to send it as 'INSERT INTO DIVISIONS (DIVISIONNAME, SORTORDER, ISDELETED) VALUES (?, ?, ?)' with params ["TestDiv", "1", "0"]'
PS: Entity is auto generated from database
If anybody wants more info ill happily provide
Ok so if anybody reaches this point and is still stuck i find kind of a work around:
I changed my Entity to look like that:
<?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;
}
}
Then in the Controller i added the following function:
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];
}
Now this function is used to get the Max id number.. so my controller would look something like that:
<?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];
}
}