⚠ 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

Callback before insert and update ignored or not working



Neo Fethi Ch

Neo Fethi Ch
  • profile picture
  • Member

Posted 07 February 2017 - 17:21 PM

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: 
ab592db9eac1468b92120894955d72d4.png

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 !


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 08 February 2017 - 09:11 AM

Hi there,

 

I dont really see any issue as why the code should be skipping the callbacks. But since it is. . and i trust u on that ;) ... i will recommend you do some debuging. If u kill the callback with a die() with some debug message .... it will fail insertion process there itself and wont do an insert - rather on the network call panel u will be able to see the output of the die debug message. If you are able to see the same there - you surely can verify it being called .. and if not. then its another issue u need to look up into the library - where it dose perform the callback before insert - there u need to identify as why the call is not calling up the given function. This can be performed only through debuging further as required.

 

Hope u reach to your solution quickly..

 

Happy GCing :)


Neo Fethi Ch

Neo Fethi Ch
  • profile picture
  • Member

Posted 09 February 2017 - 10:25 AM

Hi Amit,

Ater applying your recommendation, the functions are officially not called, only _callback_update_data and _callback_insert_data are not called, the tag callbacks are called. And i see also that $post_array is not returning session admin id on its variable...

I really don't understand where is the problem.

Anyway Thank you for your help !