Extending Mjoin
Extending Mjoin
I have a number of one-to-many joins in my product database, for a site I am updating
For something like product groups, this approach works really well, i can assign a product to as many groups as I need.
I have followed the MJoin example and successfuly updated the datatable and editor on the page.
For product colours though, this doesn't quite work as I need
As an example, I have product colour options, so there is a tblproduct table (parent), tblproductcolour table (link) and refcolour (child) table
To complicate things a little, the tblproductcolour link table also contains a price field, as each product / colour combination can potentially have a unique price.
if, when editing a product, i add or remove a product colour from my options, not only is the row removed, but all of the existing prices are set to zero
is it possible to add the price field in my link table in my mjoin ?
i have a test page at http://test.forthwebsolutions.com/hutch4dmin/products.php
This question has an accepted answers - jump to answer
Answers
Could you show me the PHP code and SQL schema you are using for this please? The key thing with Mjoin is that it doesn't do clever merging of rows - it will delete rows and then insert new ones rather than doing some kind of diff. Thus any meta information (including, I suspect in this case the price field) will be lost.
One way around this is to introduce a link table. That link table would link between the products and colours, and have only two columns - the primary key from each of the host tables. Then the delete and add can happen without problems.
Allan
the php code to generate the ajax json (extract)
the categories uses a link table tblproductcategories and works perfectly
as it has a autoincrement PK and only 2 other fields linking productid and categoryid, exactly as outlined in your comment
the colours link table also has a price field, so the price column in the link table is not visible to the editor
Thanks (and sorry for the delay)!
The issue is that the link table (
tblproductcolour
) should not include anything other than the link information. (i.e.ProductID
andColourID
in this case). Because of the way Editor's MJoin works it will destroy the row as it is editing a row (MJoin doesn't actually do an edit at all - it always does a delete then create).I think for this to work with the Editor libraries you'd need to introduce a link table between the
tblproductcolour
andtblproducts
tables I'm afraid. But it wouldn't make declaring a product / colour / price all that easy.Let me think about this one a bit...
Allan
Thanks Allan,
As a workaround, I have added insert and delete queries on the ajax/json source pages which fire on postCreate() and postRemove() to populate/de-populate the 'link' tables when a product is added or removed
Not quite as neat a solution, but the user gets a satisfactory experience and the data integrity seems to be assured ;-)
+1
Running into a similar problem
My linker table has more than 2 columns, it has two additional columns which would also need to be updated during the delete/insert sequence (one of the columns also stores a userID to track who inserted the row)
I'm afraid I don't have a solution for this yet. It would really need some kind of diff to be done on the link table, but I feel that would be fought with potential errors which might end up corrupting data.
It is something I'm keen to address, but I've not figured out how to do it nicely yet.
Allan
+1, this would be helpful for me as well. I plan to just use postCreate functions as well, though of course this is not ideal
Crush123, would it be ok for you to share your workaround?
ok, i will try.
in the scenario above, i wanted the option to add a colour/price option to my products (sheds), so each shed model/colour option could potentially have a different price
in the ajax data source, i no longer used the mjoin for this, instead i used server side events to update the related tables.
for example, on creating a new product, I had a checkbox option in my editor instance, to populate the product colours table with default values from an existing reference list, eg
the addProductColours function is like this
this then gives me (potentially) all of the colour options and default prices for this shed
I then use a separate page and editor to amend these default colour prices as required.
Conversely, when a product is deleted, i call another function to remove the colours and prices from the linked table
Hope this helps