我有两个实体:门票和类别
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;
}
在我的门票控制器中创建新票证时,我想创建一个新类别,其中tickets.id = categories.id_ticket
$ticket= new tickets();
$category= new categories();
$ticket->setCategories($categories);
$em->persist($categories);
$em->persist($ticket);
$em->flush();
我收到此错误:
SQLSTATE [23000]:[Microsoft] [SQL Server Native Client 10.0] [SQL Server]当IDENTITY_INSERT设置为OFF时,无法在表'categories'中为identity列插入显式值。
它尝试做的插入是:
INSERT INTO categories(id_ticket, ... , **id**) VALUES (?, ..., **?**)
Parameters: { 1: 'null', ..., **18: 'null'** }
我做错了什么?我不知道为什么它试图插入“id”。我知道这就是我收到错误的原因,但我是symfony2和doctrine的新手,我不知道如何解决它。
答案 0 :(得分:1)
您的拥有方存在问题(查看here)
尝试:
$ticket= new tickets();
$category= new categories();
$categories->setTicket($ticket);
$em->persist($ticket);
$em->persist($categories);
$em->flush();