I have two entities: tickets and categories
class tickets
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
/**
* @ORM\OneToOne(targetEntity="categories", mappedBy="ticket")
*/
private $categories;
public function setCategories($categories)
{
$this->categories= $categories;
}
public function getCategories()
{
return $this->categories;
}
}
class categories
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer $id_ticket
*
* @ORM\Column(name="id_ticket", type="integer")
*/
private $id_ticket;
...
/**
* @ORM\OneToOne(targetEntity="tickets", inversedBy="categories")
* @ORM\JoinColumn(name="id", referencedColumnName="id_ticket")
*/
private $ticket;
public function setTicket()
{
$this->ticket= $ticket;
}
public function getTicket()
{
return $this->ticket;
}
In my tickets controller when I create a new ticket I want to create a new category where tickets.id = categories.id_ticket
$ticket= new tickets();
$category= new categories();
$ticket->setCategories($categories);
$em->persist($categories);
$em->persist($ticket);
$em->flush();
I get this error:
SQLSTATE[23000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot insert explicit value for identity column in table 'categories' when IDENTITY_INSERT is set to OFF.
And the insert it tries to do is:
INSERT INTO categories(id_ticket, ... , **id**) VALUES (?, ..., **?**)
Parameters: { 1: 'null', ..., **18: 'null'** }
What am I doing wrong? I don't know why it's trying to insert "id". I know this is why I get the error, but I am a newbie on symfony2 and doctrine and I don't know how to solve it.