Deletion in a field other than the main identifier

I am trying to remove a list of records from my table that matches a specific id. I get the id and send it via an ajax function to the controller and use the Delete function on the model to delete a specific entry. But records are not deleted.

This is my ajax function in a view file called report.ctp where I call the controller function when the link is clicked.

$(".delete_entries").click(function() {
    $.ajax({
        type: "POST",
        url: "http://localhost/FormBuilder/reports/deleteEntries",
        data: "formid="+formid,
        async: false,
        success: function(msg){
            alert( "Data Saved: " + msg);
        }  
    });//ajax
});

      

This is the delete action in report_controller.php

function deleteEntries()
{
    $this->data['Result']['form_id']=$this->params['form']['formid'];
    $this->Report->Result->delete($this->data['Result']['form_id']);
}

      

The table from which I want to delete records is "Results". someone helps me on how to delete entries.

EDIT

Now I am using sql delete query per se to delete records in the results table.

$this->Result->query("delete from results where form_id=".$this->data['Result']['form_id']);

      

I don't know why CakePHP's delete command isn't working.

But now the problem is that only when I refresh the page is the deletion of records reflected. If I don't update, the records still show up in the table. Perhaps if CakePHP's uninstall feature works, the page will be refreshed.

Decision

The deleteAll method works as I am not specifying the primary id as input for the delete method as deceze pointed out.

$ this-> Result-> deleteAll (array ('Result.form_id' => $ this-> data ['Result'] ['form_id']));

Regarding the problem of reflecting deletion of entries, I did $ (". Entries) .remove () on success, as suggested by Xr, so the entry table is deleted without having to refresh the page.

+2


source to share


4 answers


"delete from results where form_id=".$this->data['Result']['form_id']

      

The problem is that you want to delete in a field other than the main one id

. The method Model::delete()

expects you to give it a main field id

.

When you try to do

$this->Report->Result->delete($this->data['Result']['form_id']);

      

even if put form_id

into a nice array first, the result is:

$this->Report->Result->delete(25);  // let say 'form_id' is 25

      



This indicates delete()

delete Result

with 25, it doesn't know what you want to delete based on another criterion and doesn't care. id

What you are looking for is Model::deleteAll()

.

deleteAll(mixed $conditions, $cascade = true, $callbacks = false)

Same as del () and remove (), except that deleteAll () deletes all records that match the specified conditions. The $ conditions array must be represented as a slice or SQL array.

You should do something like this:

$this->Result->deleteAll(array('Result.form_id' => $this->data['Result']['form_id']));

      

+5


source


$this->Report->delEntries($data);

      

Must not be



$this->Report->delEntries($this->data);

      

I didn't look much further, but $ data doesn't look the way it is defined in deleteEntries ().

+1


source


function deleteEntries () {

     App::import('Sanitize');
     $post = Sanitize::clean($_POST);

     //very important to set cascade to false
     $this->Result->delete($post['form_id'],false);

      

}

0


source


The second problem might be best asked in a separate question, but here it says:

As stated earlier, the table won't magically change just because your database has changed. You will need to refresh the page or delete the deleted post using Javascript.

First of all, your Controller method should return whether the deletion was successful. You could just do:

function delete() {
    $this->layout = 'ajax';

    if ($this->Result->deleteAll(...)) {
        echo "success";
    } else {
        echo "failure";
    }
}

      

To be MVC correct, you must return this value in a view, preferably in JSON format, but that will be done for an exercise.

On your site, let's say you have this table:

<table>
  <tr>
    <td>Id: 25</td><td>My result</td><td><a class="delete_entries">Delete Entries</a></td>
  <tr>
  ...
</table>

      

In your Javascript that is attached to the link in line, your callback success

will look something like this:

$(".delete_entries").click(function() {

    // save a reference to the link, "this" will be something else in the callback
    var deleteLink = this;

    $.ajax({
        ...
        success: function(data){
            if (data == "success") {
                // remove the row in which the link resides
                $(deleteLink).parents('tr').remove();
            }
        }
    }
}

      

0


source







All Articles