⚠ 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

Problem with callback_before_insert



Dale

Dale
  • profile picture
  • Member

Posted 18 April 2013 - 06:42 AM

I'm building a cross company system where people within one company can create new locations for their company only. 

 

The admin view allows you to see all the fields (including the different companies), and insert new rows successfully.

 

For a person in a company I need to restrict the form so they don't see other companies, and then insert their company (which I have from their login) after they have submitted the form.

 

I thought the best way to do this would be to have a callback_before_insert....

 

 

 
            $crud->callback_before_insert(function($post_array){
                $post_array['company'] = $company_id;
                return $post_array;
            });

 

 

When I add a new location I get "loading, saving data" appearing momentarily, and then...nothing - the table has not been updated with the new location information.

 

Any ideas?


mavershim

mavershim
  • profile picture
  • Member

Posted 18 April 2013 - 07:33 AM

Have you read the documentation regarding the callback_before_insert.

 

see sample below

 

$crud->callback_before_insert(array($this,addcompany));

 

function addcompany($post_array){
             $post_array['company'] = $company_id;
             return $post_array;
 }  

Dale

Dale
  • profile picture
  • Member

Posted 18 April 2013 - 07:51 AM

Yes I have. Have you read the example that shows you can pass anonymous functions directly if you are using PHP >= 5.3?

 

From the examples...

 

$crud->callback_before_insert(function($post_array){
$post_array['user_id'] = $this->session->userdata('user_id');
return $post_array;
});

 

BTW I have tried both, but neither work (for me)


mavershim

mavershim
  • profile picture
  • Member

Posted 18 April 2013 - 08:42 AM

there might be some error occuring on your callback.. have you tried to see if your session is working. maybe your table will not allowed no data pasing to the user_id field. try the code with static value.


davidoster

davidoster
  • profile picture
  • Member

Posted 18 April 2013 - 09:14 AM

Try using the where function  $crud->where($user_company_from_login_details, $field_company);

and see what happens.


Dale

Dale
  • profile picture
  • Member

Posted 18 April 2013 - 09:51 AM

Mavershim - I figured it out then came back and saw you had the same hint to put in a literal value (which does work).

 

The problem turned out to be my misunderstanding of closures - I thought that they would automatically have access to variables outside the immediate scope (as other languages do).

 

The solution was to use the proper PHP syntax (with "use")...

 

 

$crud->callback_before_insert(function($post_array) use ($company_id){
      $post_array['company'] = $company_id;
      return $post_array;
});

 

Thanks for your suggestions guys,