Composite key and form

I have the following associations in my database (simplified version):

db schema

This is a Many-To-Many association, but with an attribute in the join table, so I need to use One-To-Many / Many-To-One .

I have a form in which I can add as many relationships as I want for one order item and create it at the same time (mainly using How to insert a collection of forms from the documentation.

When I submit the form, I get the following error:

An entity of type TEST \ MyBundle \ Entity \ Relation has an identifier through the foreigner TEST \ MyBundle \ Entity \ Order, however this object does not have self-identification. You must call EntityManager # persist () on the associated object and verify that an identifier has been created before trying to save TEST \ MyBundle \ Entity \ Relation. If you create alerts in a Post Post (e.g. MySQL Auto-Increment or PostgreSQL SERIAL), this means that you need to call EntityManager # flush () between both persistent operations.

I understand this error because Doctrine is trying to save an object Relationrelated to order, since I have an option cascade={"persist"}in relation OneToMany. But how can I avoid this behavior?

cascade={"persist"} , ( flush(), , , ).
detach() Relation flush(), .

+5
3

Relation ( , ).
, , , , .

Relations:

/**
 * Relation
 *
 * @ORM\Entity
 */
class Relation
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Contact", inversedBy="relation")
     */
    protected $contact;

    /**
     * @ORM\ManyToOne(targetEntity="Order", inversedBy="relation")
     */
    protected $order;

    /**
     * @var integer
     *
     * @ORM\Column(name="invoice", type="integer", nullable=true)
     */
    private $invoice;

    //Rest of the entity...

cascade={"persist"} OneToMany Order:

/**
 * Orders
 *
 * @ORM\Entity
 */
class Order
{   
    /**
     * @ORM\OneToMany(targetEntity="Relation", mappedBy="order", cascade={"persist"})
     */
    protected $relation;

    //Rest of the entity...

Et voilà!

+1

, : 1) , 2) , 3) - , "" . , , , , .

, , .

/** @Entity */
class Order
{
    /** @OneToMany(targetEntity="OrderItem", mappedBy="order") */
    private $items;

    public function __construct(Customer $customer)
    {
        $this->items = new Doctrine\Common\Collections\ArrayCollection();
    }
}

/** @Entity */
class Product
{
    /** @OneToMany(targetEntity="OrderItem", mappedBy="product") */
    private $orders;
    .....

    public function __construct(Customer $customer)
    {
        $this->orders = new Doctrine\Common\Collections\ArrayCollection();
    }
}

/** @Entity */
class OrderItem
{
    /** @Id @ManyToOne(targetEntity="Order") */
    private $order;

    /** @Id @ManyToOne(targetEntity="Product") */
    private $product;

    /** @Column(type="integer") */
    private $amount = 1;
}

, , Order , OrderItem s, OrderItem, Order Entity ( /SQL ), Doctrine EntityManager Order, OrderItem ( , ). , , , Order. . , , Order, Order . ArrayCollection $items

class Order
{
    .....
    public function setItemsArray(Doctrine\Common\Collections\ArrayCollection $itemsArray = null){
    if(null){
        $this->items->clear();
    }else{
        $this->items = $itemsArray;
    }
    ....
}

, Order.

//get entity manager
$em = $this->getDoctrine()->getManager();
//get order information (with items)
$order = $form->getData();
//pull out items array from order
$items = $order->getItems();
//clear the items from the order
$order->setItemsArray(null);
//persist and flush the Order object
$em->persist($order);
$em->flush();

//reintroduce the order items to the order object
$order->setItemsArray($items);
//persist and flush the Order object again ):
$em->persist($order);
$em->flush();

, ( ). , , . , , , , .

+3

, . 100% .

I assume from the chart that you are trying to add and organize, and the relationship to the contact at the same time? If so, you need to save and discard the order before you can persist and clear the relationship. Or you can add a primary key to the relationship table.

+1
source

All Articles