Hi all,
It's been long time that i'm working with Grocery Crud in Codeigniter, but this week i had a serious problem with my callbacks.
When i set callbacks on my specific next code, it seems to be ignored. and the data dont be updated on inserted in Database Table
Especially for _callback_update_data and _callback_insert_data.
I tried all of possibility but it didn't work.
Here is my Database table:
And here is my code :
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Articles extends CI_Controller { function __construct(){ parent::__construct(); $this->load->database('default'); $this->load->model("admin_model"); $this->load->model("grocery_crud_model"); $this->load->helper("text"); $data=array(); } public function index(){ $data['validation']=""; if($this->session->userdata('admin_id')){ $privilege=explode(',',$this->session->userdata('access')); if(!in_array('articles',$privilege)){ redirect('ad_access'); } }else{ redirect('ad_access/'); } $crud = new grocery_CRUD(); $crud->set_language('french'); $crud->set_table('articles')->order_by("date_ajout","desc"); $crud->set_theme('datatables'); $crud->set_subject('Article'); $crud->fields("titre","tag_titre_url","content","image","date_ajout","date_edition","auteur","editeur","id_auteur","categorie","mots_cle"); $crud->required_fields("titre","content","image","categorie"); $crud->columns("image","titre","auteur","editeur","date_edition"); $crud->callback_before_update(array($this,"_callback_update_data")); $crud->callback_before_insert(array($this,"_callback_insert_data")); $crud->set_field_upload("image","assets/img"); $crud->callback_before_upload(array($this,"_callback_image_article")); $crud->display_as("titre","Titre"); $crud->display_as("content","Contenu"); $crud->display_as("image","Image principale"); $crud->set_relation("categorie","categories","nom_categorie"); $crud->change_field_type("date_ajout","invisible"); $crud->change_field_type("date_edition","invisible"); $crud->change_field_type("auteur","invisible"); $crud->change_field_type("editeur","invisible"); $crud->change_field_type("id_auteur","invisible"); $crud->unset_texteditor("mots_cle"); $crud->callback_before_insert(array($this,"_callback_tag_insert")); $crud->callback_before_update(array($this,"_callback_tag_update")); $output = $crud->render(); $data['output']=$output; $this->layout->set_theme('articles'); $this->layout->view('articles',$data); } function _callback_image_article($files_to_upload, $field_info){ $ext = ''; foreach ($files_to_upload as $value) { $ext = strtolower(pathinfo($value['name'], PATHINFO_EXTENSION)); $allowed_formats = array("jpg", "jpeg", "png", "gif"); if (!in_array($ext, $allowed_formats)) { return 'L\'image du champ'.$value['field_name'].'doit etre de type (.jpg, .jpeg, .png .gif)'; } } } function _callback_tag_insert($post_array){ if($post_array['tag_titre_url']==""){ $converted_tag=convert_accented_characters(url_title($post_array['titre'])); $post_array['tag_titre_url']=$converted_tag; } $post_array['id_auteur']=$this->session->admin_id; return $post_array; } function _callback_tag_update($post_array, $primary_key){ if($post_array['tag_titre_url']==""){ $converted_tag=convert_accented_characters(url_title($post_array['titre'])); $post_array['tag_titre_url']=$converted_tag.$primary_key; } return $post_array; } function _callback_update_data($post_array,$primary_key){ $post_array['date_edition']=date("Y-m-d H:i:s"); $post_array['editeur']=$this->session->username; return $post_array; } function _callback_insert_data($post_array){ $post_array['date_edition']=date("Y-m-d H:i:s"); $post_array['date_ajout']=date("Y-m-d H:i:s"); $post_array['editeur']=$this->session->username; $post_array['auteur']=$this->session->username; $post_array['id_auteur']=$this->session->admin_id; return $post_array; } }
I also tried to call_user_func and it said that function is here, so logically it seems that the two functions are not called on execution.
Thank's to help !