2

I'm doing a ManyToMany Unidirectional relation from Orders to Products that one order can have many products and one product can be in many orders.

See my code:

/**
 * @ManyToMany(targetEntity="Product")
 * @JoinTable(name="dc_order_products",
 *      joinColumns={@JoinColumn(name="order_id", referencedColumnName="id")},
 *      inverseJoinColumns={
 *          @JoinColumn(name="product_id", referencedColumnName="id"),
 *          @JoinColumn(name="product_price", referencedColumnName="product_price")     
 *      })
 **/
protected $order_products;

I didn't find in all the google how to use multiple @JoinColumns in inverseJoinColumns so i just copied the first one and separated by comma.

The problem is, when i run the project with this code:

$p = new \Entities\Product(get_date(), 'Product Name', 'Description', 39, 85, 0, 0, 1);
$em->persist($p);
$em->flush();

$o = new \Entities\Order($u->getUserId(), get_date(), 1, 150);

$o->addOrderProduct($p);

$em->persist($o);
$em->flush();

It gives to me:

Integrity constraint violation: 1048 Column 'product_price' cannot be null

But the price is defined, i did a print_r in product object and there it was.

What i missing?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

1

I've got the solution for this, just opened Doctrine FAQ and there it was :)

Look: http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html

I need to create another Entitie called OrderProducts that contains all my order products with composite primary keys relation.

Got it! Thanks.