Product:
- id
- name
- seo_name
- ...
Category:
- id
- name
- ...
Product_Category
- product_id
- category_id
- status (0 = None, 1 = Relation, Default = 1)
Create new product: P1 - categories = C1, C2
DB flow: insert product, got new id, insert relationship.
Edit product P1: Select categories C3, C4; deselect C2
We have two ways to update product data.
1. Remove all and re-add:
- Delete all records: item_id = id(P1). Soft delete: UPDATE query to set status = 0. Hard delete: DELETE query.
- Insert new records: (P1, C1), (P1, C3), (P1, C4)
2. Do not remove all. Update if record exists
[info] Current categories: C1, C2. New categories: C2, C3, C4.
- Hard: Delete (P1, Ci) Ci NOT IN (C2, C3, C4). Soft: Update status = 0 for (P1, Ci) Ci NOT IN (C2, C3, C4)
- For each category Cj in (C2, C3, C4): Check (P1, Cj), if it exists, UPDATE, else INSERT
Title:
How to update relationship between product and categoy
Description:
Product: - id - name - seo_name - ... Category: - id - name - ... Product_Category - product_id - category_id - status (0 = No...
...
Rating:
4