Question

First of all, I have this DB schema:

DB schema

I have categories, which contain products. I also have attributes (for example "color"), which contain values (for example "red", "green", etc).

So, finally, I can assign these attributes to products through the table ProductAttributeValues, that associates an attribute value to a product, this table is automatically created by Doctrine, because Product and AttributeValue have a ManyToMany relation between them.

This way I can have a category (cars) with products (Renault Megane, Ford Focus, etc). Each product can have multiple attributes with their values (color: gray, engine type: gasoline, etc). In this last example, the color is an Attribute, and gray is an AttributeValue. The engine type is another Attribute, and gasoline is an AttributeValue.

Ok, once I have my entities filled up with data (categories, attributes, attribute values, and products), how can I create a form to associate attributes and values for a given product?

More or less, the form should look like this:

form

So, as I'm in the "cars" category, I must display all attributes that belong to this category (engine and color). At the same time, I must display all values for these attributes (gasoline, diesel, electric... for the engine, and gray, red, blue... for the color).

Was it helpful?

Solution

You are not looking for collection field-types but entity field-types with a queryBuilder if you are trying to build a form like the one on your image.

With the entity field-type you can group your attributes by i.e. car.category.

... or you can use two entity fields and filter the results ( i.e. only showing attributes for category gas ) for the other selectbox in the field's queryBuilder. Best achieved by using an injected category property in your form builder being used in the queryBuilder's ->where() statement.

The collection form-type would be needed if you wanted to add/remove/edit multiple new categories or attributes to existing ones. but then your form would look different obviously.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top