Jump to content


[SOLVED] callback_before_delete not working

delete

  • Please log in to reply
5 replies to this topic

#1 tofayelahmed

tofayelahmed

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts

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.

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,059 posts
  • LocationLondon

Posted 16 April 2012 - 07: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.
Posted Image

#3 tofayelahmed

tofayelahmed

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts

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.

#4 xxaxxo

xxaxxo

    grocery CRUD Expert

  • Members
  • PipPipPip
  • 71 posts
  • LocationBulgaria

Posted 17 April 2012 - 05:09 AM

+1 for sending this topic to FAQ :)

#5 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,059 posts
  • LocationLondon

Posted 17 April 2012 - 06:19 AM

You are right xxaxxo and here it is now :)
Posted Image

#6 huiquan93

huiquan93

    Newbie

  • Members
  • Pip
  • 1 posts

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.







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users