Saturday, 10 August 2013

Automatically create and update one to one relationships in MySQL

Automatically create and update one to one relationships in MySQL

I have a web shop (zen-cart)that has a very inefficient attribute schema.
One option can be used for attributes for many products. (Instead of the
other way around). I have tried to use views to make things easier, but
creating new attributes is still very tedious. We have looked into
changing the php-code, but it is too difficult. The solution is also
multi-lingual, something I don't need.
The tables are the following:
tb_products:
prod_ID
tb_description:
prod_ID
lang_ID
product_name (T-shirt)
tb_attributes:
attr_ID
prod_ID
option_ID
values_ID
tb_options:
option_ID
language_ID
option_name (Size & Color)
tb_values:
value_ID
option_ID
language_ID
value_name (XS Red)
As a quick-fix, I would like to do the following:
Make the additional fields:
tb_products:
product_name
option_name [optional - only if attributes are to be created]
value_concatinator [optional]
tb_attributes:
value_name_1
value_concatinator_1 [optional]
value_name_2 [optional]
value_concatinator_2 [optional]
value_name_3 [optional]
2.
Create a trigger, so that when I create a new row in tb_products, this
will automatically create a new row in both tb_description and tb_options,
with respectively product_name and option name taken from tb_products.
Every time these fields are updated in tb_products, they must also be
updated in tb_description and tb_options. Probably let the option_ID be
the same as prod_ID
3. When creating a new row in tb_attributes, automatically: - Insert the
option_ID from the parental product row. - Create a new row in tb_values.
value_name is a concationation of the value names and value_concatinators
in attributes
CONCAT(value_name_1, ' ' , value_concatinator_1, ' ', value_name_2, ' ',
value_concatinator_2, ' ', value_name_3) FROM tb_attributes
Every time these fields are updated in tb_attributes, they must be
automatically updated in tb_values
So my main object is to actively only update the tables tb_products and
tb_attributes, and let MySQL update the other tables every time a product
or attribute is created or updated in the new fields mentioned above.
I am not sure if triggers are the answer, or if the columns themselves can
be made to be automatically updated (and if the last solution will make
some problems with how the columns are defined in the php-code.
For those of you who would like to make the values_names into an array:
that is how it is supposed to be. But it doesn't work out with the
quantities, so that customers can then actually add combinations that
don't exist. People have spent 1000s of hours trying to fix it. To no
avail. Now all I want is a solution that actually works backoffice-wise,
and makes it easy for me to sort etc.
We use Filemaker and MagneticOne for normal backoffice activities, MySQL
Workbench for changes to the database. I can read php, but the code is too
complex for me to change (and even professionals make a lot of mess when
trying to upgrade to newer versions, due to necessary customizations)

No comments:

Post a Comment