views:

854

answers:

5

I am trying to delete a list of entries form my table corresponding to a particular id. I get the id and post it through an ajax function to the controller and use the Delete function of the model to delete the particular entry. But the entries are not deleted.

This is my ajax function in the view file called reports.ctp where I call the controller function when a 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 the reports_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 the entries is 'Results'. some one help me as to how to delete the entries.

EDIT

Now I'm using the delete sql query as such to delete the entries in the Results table.

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

I do not know why the delete command of CakePHP doesn't work.

But now the problem is, only when I refresh the page, the deletion of the entries are reflected. If I do not refresh, the entries are still displayed in the table. Maybe if the delete function of CakePHP works, the page will get refreshed.

SOLUTION

The deleteAll method works since I'm not giving the primary id as the input for the delete method as deceze has pointed out.

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

Regarding the problem of reflecting the deletion of entries, I did a $(".entries).remove() on success as Xr had suggested, so the table of entries is removed without having to do a page refresh.

+1  A: 
$this->Report->delEntries($data);

Shouldn't this be

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

I didn't look much further than that, but $data doesn't look like it's defined in deleteEntries().

Xr
ya I changed that to $this->data. But still it doesn't work. I dont get the data in the model.
Angeline Aarthi
Then, if $this->params['form']['formid'] has the right value (I'll assume the Javascript variable 'formid' is defined but your code doesn't mention it), I don't know why the data isn't passed to the model. As a side note, unless you actually have more logic than what you have pasted here, you could as well run $this->Report->Result->delete($this->params['form']['formid']) directly from ReportsController.
Xr
@Xr: No specific reason for having the delete command in the model. Just thought that maybe the delete command works only in the model. Now I've changed as you have said, still the entry isn't deleted.
Angeline Aarthi
Regarding your second problem, the one regarding the lack of refresh in the table, it's quite logical since there is no full page reload (CakePHP does not matter here). So the browser doesn't know the row should be removed. In your success handler, add something like $(this).remove(). Depending on your code, you might have to remove a parent of the clicked element instead of the element itself. Use $(this).parent() or $this.parents(). This way, on success, you remove the row (you don't reload anything, you just pretend you did).
Xr
@Xr: Your suggestion works great. I did $(".entries").remove() on success where entries is the class that holds the table. Now the table is removed without page refresh.. Thank you..
Angeline Aarthi
A: 

In your model the code should be

$this->delete($this->data);
dunlop
That's not the signature of Model::delete(int $id = null, boolean $cascade = true) < http://book.cakephp.org/view/516/Deleting-Data >
Xr
A: 

function deleteEntries() {

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

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

}

Funky Dude
It's only important if you don't want it to cascade, not as a rule of thumb.
deceze
+4  A: 
"delete from results where form_id=".$this->data['Result']['form_id']

The problem is that you want to delete on a field other than the primary id. The Model::delete() method expects you to give it the primary id field.

When you're trying to do

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

even if you put the form_id into a nice array first, the result is just this:

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

That tells delete() to delete the Result with the id 25, it doesn't know that you want to delete based on a different criterium, nor does it care.

What you're looking for is Model::deleteAll().

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

Same as with del() and remove(), except that deleteAll() deletes all records that match the supplied conditions. The $conditions array should be supplied as an SQL fragment or array.

You should be able to do something like this with it:

$this->Result->deleteAll(array('Result.form_id' => $this->data['Result']['form_id']));
deceze
Well,I did try deleteAll yesterday.. But didn't get the entries deleted. I'll try once again.
Angeline Aarthi
yes, deleteAll works perfectly.. Earlier I had given as array('conditions'=>array('Result.form_id' => $this->data['Result']['form_id'])).. that's why it didn't delete the entries I suppose..THank You..
Angeline Aarthi
A: 

Your second problem may be best put in a separate question, but here goes:

As was pointed out before, the table won't magically change just because your database changed. You'll have to refresh the page or remove the deleted entry via Javascript.

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

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

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

To be proper MVC you should return this value in a View, preferably in JSON format, but it'll do for the 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, which is attached to a link in the row, your success callback would 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();
            }
        }
    }
}
deceze