Prevent row deletion on joined tables
Prevent row deletion on joined tables
jbronikowski
Posts: 7Questions: 4Answers: 0
How do you prevent rows from deleting from a left join table when you delete a row? I would like to delete a row from sppScheduleNetwork but not Site.
Editor::inst( $db, 'sppScheduleNetwork', 'siteID' )
->debug( true )
->fields(
Field::inst( 'sppScheduleNetwork.siteID' )
->validator( 'Validate::numeric' ),
Field::inst( 'sppScheduleNetwork.notes' ),
Field::inst( 'sppScheduleNetwork.tenativeActivationDate' )
->validator( 'Validate::dateFormat', array( 'format'=>'m/d/y' ) )
->getFormatter( 'Format::date_sql_to_format', 'm/d/y' )
->setFormatter( 'Format::date_format_to_sql', 'm/d/y' ),
Field::inst( 'sppScheduleNetwork.confirmedActivationDate' )
->validator( 'Validate::dateFormat', array( 'format'=>'m/d/y' ) )
->getFormatter( 'Format::date_sql_to_format', 'm/d/y' )
->setFormatter( 'Format::date_format_to_sql', 'm/d/y' ),
Field::inst( 'sppScheduleNetwork.actualActivationDate' )
->validator( 'Validate::dateFormat', array( 'format'=>'m/d/y' ) )
->getFormatter( 'Format::date_sql_to_format', 'm/d/y' )
->setFormatter( 'Format::date_format_to_sql', 'm/d/y' ),
Field::inst( 'Site.site_name' ),
Field::inst( 'Site.region' ),
Field::inst( 'Site.site_city' )
)
->leftJoin( 'Site', 'sppScheduleNetwork.siteID', '=', 'Site.site_id' )
->process( $_POST )
->json();
This discussion has been closed.
Answers
Editor shouldn't be deleting the left joined table at all. Can you show me the JSON being returned from the server after a delete action? Is it possible the database has
ON CASCADE DELETE
for that reference?Allan
That does indeed show that Editor is attempting to delete from the joined table. That's very odd, since it doesn't do that here.
Can you show me your full Javascript as well please?
What version of Editor are you using on the client and server-side?
Allan
/*! DataTables Editor v1.6.3
*
* ©2012-2017 SpryMedia Ltd, all rights reserved.
* License: editor.datatables.net/license
*/
/**
* @summary DataTables Editor
* @description Table editing library for DataTables
* @version 1.6.3
* @file dataTables.editor.js
* @author SpryMedia Ltd
* @contact www.datatables.net/contact
*/
Could you try upgrading to 1.6.5 on both the client and server-side please?
Thanks,
Allan
Hi
Was this problem fixed?
I have just discovered same using Oracle.
When joined two tables having same key_field, Editor deletes rows from both tables during DELETE.
Problem disappeared when I created complex key adding filed which is not in other table (not sure if it does matter, but I decided to mention it).
Could you add
->debug( true )
immediately before the->process( ... )
call please? Then show me the JSON return from the server when you delete a row. It will contain the SQL statements used by Editor.Thanks,
Allan
Alan:
Thanks. Could you confirm what version of the PHP libraries you are using please? I've just tried the exact above code with 1.7.3 and it appears to function as expected.
Thanks,
Allan
Ignore that - sorry. I've just got it working (i.e. in the broken state). Its because I was throwing an error when executing against a table I don't have. I'll update hwere when I have this addressed.
Allan
It appears that the code is actually working as designed at the moment - it might be that the design has a bug though... What it is doing is seeing that you have a row in your
WI_USERS
table that is dependent upon the value in theWI_PORTAL_STAFF
table.It is incorrectly assuming that if you delete the entry from the staff table then you would also want to delete it from the users table (since if it left the number in place in the users table it would break referential integrity).
What is your intention for the
WI_USERS.USER_ID
value when you delete from theWI_PORTAL_STAFF
table? Do you want it to be set tonull
rather than being deleted?Thanks,
Allan
I would say assumption is wrong. Joined table is only a helper.
In my case I want to display/edit User from WI_PORTAL_STAFF using WI_USERS for displaying only friendly name. In DB it can be WI_PORTAL_STAFF having foreign key on USER_ID pointing to USER_ID in WI_USERS. If I would like to have associated row deleted from WI_PORTL_STAFF upon deleting from WI_USERS table, I would define foreign key with option ON DELETE CASCADE, but this is DB architect to decide what to do with orphaned rows, DataTables do not have to worry about it.
Agreed - I think this should indeed fall upon the database schema rather than attempting to do it in the PHP code.
For the moment, if you open the
Editor.php
file and search for the comment// Remove from the left join tables
- comment out that entirefor
loop and it will operate as expected.I've logged this as something to be changed.
Thanks,
Allan
Thank you Allan.