I can confirm this issue. I also expected that Doctrine would figure out to
save the Order first, and OrderItems after, when there is such a *cascade
persist* in play. I don't like that need to "*call EntityManager#flush()
between both persist operations*". I wonder if this is a bug.
One solution that worked for me, is to do give OrderItem its own
(autogenerated) @Id property. Even though it is redundant and meaningless.
The existing composite @Id can be replaced by a UniqueConstraint. The
OrderItem would then look like this:
/**
* @Entity
* @ORM\Table(uniqueConstraints = {
* @ORM\UniqueConstraint(columns = {"order", "product"}) } )
*/
class OrderItem
{
/**
* @Id @GeneratedValue(strategy = "IDENTITY")
* @Column(type = "integer", nullable = false)
*/
private $id;
/** @ManyToOne(targetEntity="Order") */
private $order;
/** @ManyToOne(targetEntity="Product") */
private $product;
/** @Column(type="integer") */
private $amount = 1;
/** @Column(type="decimal") */
private $offeredPrice;
public function __construct(Order $order, Product $product, $amount = 1)
{
$this->order = $order;
$this->product = $product;
$this->offeredPrice = $product->getCurrentPrice();
}
}
Op woensdag 2 oktober 2013 03:36:24 UTC+2 schreef [email protected]:
>
> I'm having a problem which I cannot solve, and I can't find the solution
> anywhere online.
>
> My scenario is this one exactly:
> http://docs.doctrine-project.org/en/2.1/tutorials/composite-primary-keys.html#use-case-3-join-table-with-metadata
> Assuming my entities are also Order, Product and OrderItem, here are the
> things to consider:
>
> - Order has a GeneratedValue numeric ID
> (auto-increment in MySQL)
> - OrderItem has a Composite Key with the Order, the Product and a 3rd
> integer field (not present in the link below, but I don't thing that could
> be a problem).
> - The "items" property in the Order entity, has *cascade: persist*
>
> When I try to persist the Order I get this:
> *Entity of type {{OrderItem}} has identity through a foreign entity
> {{Order}}, however this entity has no identity itself. You have to call
> EntityManager#persist() on the related entity and make sure that an
> identifier was generated before trying to persist '{{OrderItem}}'. In case
> of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL
> SERIAL) this means you have to call EntityManager#flush() between both
> persist operations.*
>
> Any clue??
> Thanks!
>
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.