dmh
dmh

Reputation: 430

callback_before_update Grocery Update Value Null

I have a Controller like this, using callback_before_update to update a field in another table, but I can't get a value from the variable $post_array, as it instead returns null; any idea why?

$crud = new grocery_CRUD();
    $crud->set_table('product');
    $crud->display_as('category_idcategory', 'Category');
    $crud->display_as('Brand_idBrand','Brand');
    $crud->set_field_upload('product_url_image','assets/uploads/files');
    $crud->set_relation('category_idcategory','category','name');
    $crud->set_relation('Brand_idBrand','brand','brand_name');

    $crud->fields('idProduk','product_name','product_url_image','Price_Class_1','Price_Class_2','Price_Class_3','category_idcategory','Brand_idBrand');
    $crud->callback_edit_field('Price_Class_1',array($this,'callback_price_1'));
    //$crud->callback_field('Price_Class_1',array($this,'callback_price_1')); 
    $crud->callback_field('Price_Class_2',array($this,'callback_price_2')); 
    $crud->callback_field('Price_Class_3',array($this,'callback_price_3')); 
    $today = date("Y-m-d H:i:s");  
    $crud->field_type('lastUpdate', 'hidden',$today);
    $crud->callback_column('Price_Class_1',array($this,'getPrice1'));
    $crud->callback_column('Price_Class_2',array($this,'getPrice2'));
    $crud->callback_column('Price_Class_3',array($this,'getPrice3'));

      $crud->callback_before_update(
    function ($post_array,$primary_key)
    {
         unset($post_array['Price_Class_1'],$post_array['Price_Class_2'],$post_array['Price_Class_3']);

      //Or do something with your post arrat here
      if($primary_key !== null)//This means that you update and not insert something
      {
          $priceUpdate = array("price" => $post_array['Price_Class_1']);//THIS ONE RETURN NULL
       //$this->db->update(....);
       $this->db->update('price',$priceUpdate,array('product_idProduk'=>$primary_key,'priceClass_idpriceClass'=>1));

      }
      else
      {
       //$this->db->insert(....);
      }

      return $post_array;

    }
);

Upvotes: 1

Views: 420

Answers (1)

Parth Ganatra
Parth Ganatra

Reputation: 138

You can not define a function anywhere in the code. Put this line and than define function outside the code.

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

Upvotes: 3

Related Questions