Author Topic: Prevent deleting rows having foreign keys  (Read 3883 times)

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Prevent deleting rows having foreign keys
« on: February 10, 2014, 04:42:58 pm »
I want to prevent users from deleting rows from a table where there are related records in another table. I'm using MySQL.

In PHP i have:

Code: [Select]
$msg=array();

    if(empty($whatever)){
        $msg['cenas']="N
« Last Edit: February 11, 2014, 09:48:22 am by paramquery »

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
My previous question got stripped off
« Reply #1 on: February 10, 2014, 04:45:36 pm »
In the attached file you may find my previous question, since the code was stripped off..

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Prevent deleting rows with records
« Reply #2 on: February 10, 2014, 08:40:23 pm »
You can think of couple of ways to do this:

1) Keep a hidden boolean column which tells you whether the row has associated records.
rowData["related"] == true / false

Either don't display delete button in the row having related records or show a message when Delete button is clicked.
Code: [Select]
column.render = function(ui){
  if(ui.rowData["related"]){
    return "";
  }
  else{
    return "<button>Delete</button>";
  }
}

2) Check Server side after deleteList is posted. In this case you don't need jQuery.parseJSON because dataType is already "json"
obj received by success callback is constructed in PHP.
Format of obj is {related => boolean, msg => string, rows=> Array of rows }

Code: [Select]
if (deleteList.length) {
                $.ajax($.extend({}, ajaxObj, {
                    data: { "deleteList": deleteList },
                    dataType:"json",
                    success: function (obj) {
                    //var obj = jQuery.parseJSON(rows);//not required.
                        if(obj.related){
                            //show message
                           alert(obj.msg);
                       $grid.pqGrid("rollback", { type: 'delete' });
                        }
                        else{     
                       $grid.pqGrid("commit", { type: 'delete', rows: obj.rows });
                        }
                    },
                    complete: function (response) {
                        $grid.pqGrid("hideLoading");
                        $grid.pqGrid("rollback", { type: 'delete' });
                    }
                }));
            }



« Last Edit: February 11, 2014, 09:51:07 am by paramquery »

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Prevent deleting rows having foreign keys
« Reply #3 on: February 11, 2014, 02:59:13 pm »
Solution 2 is working fine for now although  I think solution 1 could be more effective (less server requests).
I'll dig into it.

Thanks!
 :D