我有一个像这样的Controller,使用callback_before_update来更新另一个表中的字段,但我无法从变量$ post_array中获取值,因为它返回null;任何想法为什么?
$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;
}
);
答案 0 :(得分:2)
您无法在代码中的任何位置定义函数。把这一行放在代码之外定义函数。
$crud->callback_before_update(array($this,'function_name'));