⚠ 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

How to use callback_column functions from a helper file?



edgarvaldes

edgarvaldes
  • profile picture
  • Member

Posted 31 July 2014 - 18:23 PM

CodeIgniter lets you organize all your functions inside helpers files. This is great, because I reuse the same functions in some controllers and I have to copy/paste all of them from one to another, so the idea of using a single file is a lot better.

 

So, I need to put all my "callback_column functions" inside a helper file for a better management. But, once this is done, how can I call/use the function in the controller?

 

P.S. I am not that good with Object Oriented Programming, so please be gentle :(


skech

skech
  • profile picture
  • Member

Posted 24 February 2019 - 22:41 PM

I m answering this for any new member having the same question.So :

in case you have the desired callback function inside your controller you do this :

// Somewhere inside you controllers' method you have

$crud->callback_before_update(  array ($this, '_prepare_post_array_for_insert')  );

// and in the same controllers' file later :

function _prepare_post_array_for_insert($post_array) 
{	
 
        $CI->db->select('first_name, last_name,  fathers_name, mothers_name, birthday' );
        $CI->db->where('last_name',	$post_array['last_name'] )
	       ->where('first_name',	$post_array['first_name'] )
	       ->where('fathers_name', $post_array['fathers_name'] )
	       ->where('mothers_name', $post_array['mothers_name'] )
	       ->where('birthday', 	$post_array['birthday'] );

		$query = $this->db->get('member');

		if ( $query->num_rows()  > 0 )
		{
            	    return FALSE;                		
		}
		else
		{	
		    $post_array['member_id'] = $this->member_model->get_unused_id();
	            $post_array['created_at'] = date('Y-m-d H:i:s');
		    return $post_array;	
		}

		return $post_array;
	}       

Notice that in this case you use $this inside the  callback_before_update() function call 

 

To use it the way you want having all the callbacks inside a helper file do it like this :

 

controller file :

class Member extends CI_Controller 
{

  public function __construct()
  {
    parent::__construct();

    // Load the helper file which will have your callback functions gathered inside
    $this->load->helper('member_registration'); //file here is member_registration.php 

  }

  public function insert_member()
  {
   // some of your code here ...etc etc 

   // note $this is missing
    $crud->callback_before_insert('_prepare_post_array_for_insert'  ); 
   
   // rest of your code goes on


  }
 






}

and inside application/helpers you have a helper file named   "member_registration.php"  including something like this :

<?php if( ! defined('BASEPATH') ) exit('No direct script access allowed');


if ( ! function_exists('_prepare_post_array_for_insert'))
{
	function _prepare_post_array_for_insert($post_array) 
	{	
		$CI =& get_instance();
		$CI->db->select('first_name, last_name,  fathers_name, mothers_name, birthday' );
		$CI->db->where('last_name',	$post_array['last_name'] )
			 ->where('first_name',	$post_array['first_name'] )
			 ->where('fathers_name', $post_array['fathers_name'] )
			 ->where('mothers_name', $post_array['mothers_name'] )
			->where('birthday', 	$post_array['birthday'] );

		$query = $CI->db->get( 'member' );

		if ( $query->num_rows()  > 0   )
		{
            	     return FALSE;
                }
		else
		{	
				$post_array['member_id'] = $CI->member_model->get_unused_id();
				$post_array['created_at'] = date('Y-m-d H:i:s');
			 	return $post_array;
			
		}

        }

}

Notice that inside the helper you are using  $CI =& get_instance(); to get CIs intance and  then you are using the var  $CI instead of  $this 

Hope this helps someone