0

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.

4

1 回答 1

1

你的拥有方有问题(看这里

尝试 :

$ticket= new tickets();
$category= new categories();
$categories->setTicket($ticket);
$em->persist($ticket);
$em->persist($categories);
$em->flush();
于 2012-12-13T10:55:49.280 回答