Z
Z
zugo2015-02-21 19:40:35
MySQL
zugo, 2015-02-21 19:40:35

How to make a connection between three tables in the database (products, categories and additional fields)?

In general, there is a simple site on Yii 1.1, it has a product catalog. Products are divided into categories (many-to-one relationship). It is necessary to make it possible to add (and, of course, display in the view of a specific product) additional fields to each category (for example, if this is the category "Wallpaper" - you can add the fields "color", "texture", "footage", etc. .d.), but at the same time, obviously, it is necessary that their values ​​are filled in for each product separately.
DB Diagram:
f8efc3ad369f4bb39e85a1bb034126fb.png
Relationships in Yii Models:
Item Model:

return array(
    'category' => array(self::BELONGS_TO, 'ItemCategory', 'category_id'),
    'fields' => array(self::MANY_MANY, 'ItemField', 'items_fields_values(item_id, field_id)'),
);

ItemField Model:
return array(
    'itemCategory' => array(self::BELONGS_TO, 'ItemCategory', 'item_category_id'),
    'items' => array(self::MANY_MANY, 'Item', 'items_fields_values(field_id, item_id)'),
);

ItemCategory Model:
return array(
    'items' => array(self::HAS_MANY, 'Item', 'category_id'),
    'itemsFields' => array(self::HAS_MANY, 'ItemField', 'item_category_id'),
);

I feel that I'm doing something wrong, I've been stupid for half a day, I can't figure out what it is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
6
65536, 2015-02-22
@65536

i did this gyazo.com/5124c0f4dacc9b85c84b7f04eb79e368
products belong to categories, properties too (each category has its own set of properties), property values ​​live in a link table between properties and products.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question