callback_before_insert & callback_insert
- Single Page
Posted 08 December 2012 - 14:35 PM
protected function db_insert($state_info)
{
1. $validation_result = $this->db_insert_validation();
2. if($validation_result->success)
{
3. $post_data = $state_info->unwrapped_data;
4. $add_fields = $this->get_add_fields();
5. if($this->callback_insert == null)
{
6. if($this->callback_before_insert != null)
{
7. $callback_return = call_user_func($this->callback_before_insert, $post_data);
8. if(!empty($callback_return) && is_array($callback_return))
9. $post_data = $callback_return;
10. elseif($callback_return === false)
11. return false;
}
12. $insert_data = array();
13. $types = $this->get_field_types();
14. foreach($add_fields as $num_row => $field)
{
15. if(isset($post_data[$field->field_name]) && !isset($this->relation_n_n[$field->field_name]))
{
16. if(isset($types[$field->field_name]->db_null) && $types[$field->field_name]->db_null && $post_data[$field->field_name] === '')
{
17. $insert_data[$field->field_name] = null;
}
18. elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'date')
{
19. $insert_data[$field->field_name] = $this->_convert_date_to_sql_date($post_data[$field->field_name]);
}
20. elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'readonly')
{
21. //This empty if statement is to make sure that a readonly field will never inserted/updated
}
22. elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'set')
{
23. $insert_data[$field->field_name] = !empty($post_data[$field->field_name]) ? implode(',',$post_data[$field->field_name]) : '';
}
24. elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'datetime'){
25. $insert_data[$field->field_name] = $this->_convert_date_to_sql_date(substr($post_data[$field->field_name],0,10)).
substr($post_data[$field->field_name],10);
}
26. else
{
27. $insert_data[$field->field_name] = $post_data[$field->field_name];
}
}
}
28. $insert_result = $this->basic_model->db_insert($insert_data);
29. if($insert_result !== false)
{
30. $insert_primary_key = $insert_result;
}
31. else
{
32. return false;
}
33. if(!empty($this->relation_n_n))
{
34. foreach($this->relation_n_n as $field_name => $field_info)
{
35. $relation_data = isset( $post_data[$field_name] ) ? $post_data[$field_name] : array() ;
36. $this->db_relation_n_n_update($field_info, $relation_data ,$insert_primary_key);
}
}
37. if($this->callback_after_insert != null)
{
38. $callback_return = call_user_func($this->callback_after_insert, $post_data, $insert_primary_key);
39. if($callback_return === false)
{
40. return false;
}
}
41. }else
{
42. $callback_return = call_user_func($this->callback_insert, $post_data);
43. if($callback_return === false)
{
44. return false;
}
}
45. if(isset($insert_primary_key))
46. return $insert_primary_key;
47. else
48. return true;
}
49. return false;
}
For example having this setup:
$this->grocery_crud->callback_before_insert(array($this,'_prepara_datos_agregar'));
$this->grocery_crud->callback_insert(array($this,'_insertar_registro'));
When trying to create a record, coming in on the line 41 because there is a function callback_insert (_insertar_registro).
Then this piece of code, lines 42-44, ignores the execution of the function callback_before_insert (_prepara_datos_agregar).
I understand then that there is no way to use both functions together. Is this correct?
Posted 11 December 2012 - 22:40 PM
[quote]This callback escapes the auto insert of the CRUD, and runs only the inserted callback.[/quote]
Posted 11 December 2012 - 22:56 PM
thanks
Posted 11 December 2012 - 23:29 PM