Why delete is sending all the fields to the server instead of only the ID?

Why delete is sending all the fields to the server instead of only the ID?

Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

What is the reason to send all the fields of a row to the server in case of a delete? Wouldn't it be more efficient to only send the ID's back?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    That's what Editor used to do, but there were a number of support requests for that behaviour to be added, so I did. Basically the main reason was to be able to log the data that has been deleted (exactly how is up to the developer). There were also some cases where dependent tables needed to be updated based on the deleted data, and that logic wasn't in the database.

    I agree that generally this isn't needed - I'd typically just read back what is in the database anyway, but in terms of efficiency, it makes very little difference. There is a bit more overhead in the HTTP request, but I don't expect that to make any real difference to the performance. The Editor PHP libraries don't use that data - only the IDs, but its there if anyone needs them.

    Allan

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    OK, I understand.

    But yes I think there is some overhead if you are handling Ajax with $_POST in PHP. As you know PHP has the max_input_var limit (default 1,000). I will not change $_POST in JSON type calls and I will not be able to change the value of max_input_vars as I have a shared hosting plan. So when sending only the ID's back I would be able to delete 999 rows in 1 time. But if I am sending 20 fields per row I can only delete 49 rows per time.

    But thanks for your answer.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You can use the ajax.data option to modify the data that is sent to the server. Either remove some of the fields, or more usefully have the function return the data as a JSON string - that way you can read from the input in your PHP script and send as many rows as you like (up to the max post size - not the max variables size).

    Allan

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    That's really great Allan! Thanks!.

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    @allan
    For now, I resolved it this way:

    ajax:
    {
        type: `POST`,
        url: `Ajax/Some_script`,
        data: 
            function(d)
            {
                /*
                    Data will hold the stringified results and this way we will not have
                    problems with PHP's max_input_vars limit, unless someone is setting
                    it to less than 2 ;).
                */
                return `action=` + d[`action`] + `&data={"data":` + JSON.stringify(d[`data`]) + `}`;
            }
    },
    

    I am posting this example as I lost a lot of time when I first tried to stringify 'd' which will give problems with decoding in PHP as stringify(d) returns not a string but an array.

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    And it could be shorter by using this:

                return `action=` + d.action + `&data={"data":` + JSON.stringify(d.data) + `}`;
    
  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    And the final solution is only sending keys in case of a "remove" action:

    ajax:
    {
        type: `POST`,
        url: `Ajax/Groups`,
        data: 
            function(d)
            {
                var data = ``;
    
                if (d.action == `remove`)
                {
                    var data_tmp = `{`;
                    for (var key in d.data) {
                        data_tmp += `"` + key + `":{"groups":{"id":"` + d.data[key].groups.id + `"}},`;
                    }
                    data = data_tmp.replace(/.$/,`}`);
                }
                else
                {
                    data = JSON.stringify(d.data);
                }
    
                return `action=` + d.action + `&data={"data":` + data + `}`;
            }
    },
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Nice one - thanks for sharing this!

    Allan

This discussion has been closed.