⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

[SOLVED] callback_before_delete not working



tofayelahmed

tofayelahmed
  • profile picture
  • Member

Posted 16 April 2012 - 10:40 AM

I have a table with fields acc_no, call_no and others.
I want that if i delete the one row from the list page of grocery crud, it check that if this acc_no used in another table, it show a message and not delete otherwise delete.
I use "callback_before_delete" but not working.
Here my code is attached:

$this->grocery_crud->callback_before_delete(array($this,'delete_check'));
function delete_check($post_array,$primary_key)
{
$id = $primary_key;
//$acc_no = $post_array['acc_no'];
$this->db->select('acc_no');
$this->db->where('id',$id);
$query = $this->db->get('lib_book');

foreach ($query->result() as $row)
{
$acc_no = $row->acc_no;
}
$this->db->where('acc_no',$acc_no);
$query1 = $this->db->get('booking');
if($query1->num_rows() > 0)
{
return false;
}
else
{
return true;
}

}


I don't know--is this appropriate process?
Help please.

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 16 April 2012 - 19:45 PM

First of all the parameters are wrong,you wrote:

function delete_check($post_array,$primary_key)

but the correct one is:

function delete_check($primary_key)

All the callbacks delete take only one parameter (the primary key)

Second. The most common mistake is to add the callback at

$this->grocery_crud->callback_before_delete(array($this,'delete_check'));

but write your code look like this:


$crud = new grocery_CRUD();
$crud->set_table('your_table');
$this->grocery_crud->callback_before_delete(array($this,'delete_check')); // <----THIS IS WRONG
$output =$crud->render();
...


so the right thing is to write :


$crud = new grocery_CRUD();
$crud->set_table('your_table');
$crud->callback_before_delete(array($this,'delete_check'));
$output =$crud->render();
...


or


$this->grocery_crud->set_table('your_table');
$this->grocery_crud->callback_before_delete(array($this,'delete_check'));
$output =$this->grocery_crud->render();


I just mention this thing as it is a common mistake that will make you insane until you find a solution for this one.

tofayelahmed

tofayelahmed
  • profile picture
  • Member

Posted 17 April 2012 - 04:41 AM

Thanx johnny. You are great one.
I get my output only change the
function delete_check($post_array,$primary_key) to function delete_check($primary_key).
Thanx again.

xxaxxo

xxaxxo
  • profile picture
  • Member

Posted 17 April 2012 - 05:09 AM

+1 for sending this topic to FAQ :)

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 17 April 2012 - 06:19 AM

You are right [member='xxaxxo'] and here it is now :)

huiquan93

huiquan93
  • profile picture
  • Member

Posted 08 May 2019 - 06:54 AM

Hi Guys,

 

Why the function argument don't use $post_array just like before_update_callback($post_array, $primary_key) ?

e.g.

 

function delete_check($post_array,$primary_key) {...}

 

 

 

 

Because I have many tables with different primary field's name, then i need to set every table primary field.

for example i have 50 tables different primary field, then i need to hard code 50 primary field's name 

 


 

But with $post_array, i can just take the 1st index of $post_array which is primary field even though i don't know the name.

You understand me?

 

This is just an idea.

Thanks.