0
votes

I'm using symfony 3.0 and doctrine orm 2.5, but I have some dificulties when relation product and product attributes.

Suppose that I have the database tables named ad product, product_attribute and attribute.

Firstly, each product can has many product_attributes.

The table product_attribute can be relation with many products, but has a unique attribute and value for each product_id.

The attribute table is really agnostic, and doesn`t know the existence of product and product_attribute, but the product_attribute should be a existent attribute_id on relation.

How can I make these? I tried to make a relationship between product and attributes using the pivot table "product_attribute", but this not persist the value of attribute...

Example of desired database structure: enter image description here

1

1 Answers

1
votes

Your question is a little vague with no code. However if your product is persisting, that means you called the doctrine manager to persist the product. That means you are doing something like this:

$this->getDoctrine()->getManager()->persist($product)

Then you may have added the product_attribute, and the attribute itself. The key to remember is that you must persist each object you want to save to the database, so something like this:

$this->getDoctrine()->getManager()->persist($product);
/* Add product attribute */
/* Add attribute to product attribute */
$this->getDoctrine()->getManager()->persist($productAttribute);
$this->getDoctrine()->getManager()->persist($attribute);
/* save to database */
$this->getDoctrine()->getManager()->flush();

You cannot just persist the parent object you need to persist the children to.