before_save hoock not working

edited November 2016 in Modules
i am trying to json encode all data to a single field
so i have created my advance module model from fields as :

function form_fields($values = array())
{

unset($values['last_modified']) ;
$data= (array) json_decode($values['data']);
unset($values['data']) ;
$values= array_merge($values,$data);
// print_r($values);
$fields = parent::form_fields($values);
unset($fields['data']) ;
$fields['theam']['type']='select';
$fields['theam']['required']='required';
$fields['theam']['options']= result_to_key_val($this->db->select('id,name')->from('fuel_theam')->get()->result());
$fields['theam']['default']=(isset($values['theam']))?$values['theam']:'';
$fields['contact_email']['type']='email';
$fields['theam']['contact_email']=(isset($values['contact_email']))?$values['contact_email']:'';

return $fields;
}
working just fine ...
but while saving the data i want to json_encode them just before saving to data field :
now that is not working :
i have defined hook in my "application/config/hooks.php" file as follow :

$hook['before_save_epssetting'] = array(
'class' => 'Epssetting_model',
'function' => 'before_save_epssetting',
'filename' => 'epssetting_model.php',
'filepath' => 'models',
'params' => array()
);

in fuel\modules\epssetting\models\epssetting_model.php


public function before_save_epssetting($values)
{
print_r($values); die();
return $values;
}
that is not even hitting it :
it it hitting the function

function on_before_save($values)
{
print_r($_POST);
// print_r($values); die();
return $values;
}
that is also after executing the update query with an failure:
-----------------------------------------------------

A PHP Error was encountered

Severity: Notice

Message: Undefined index: data

Filename: models/epssetting_model.php

Line Number: 61
Array ( [fuel_other_items] => [fuel_restore_version] => [fuel_restore_ref_id] => 1 [theam] => 1 [contact_email] => [id] => 1 [last_modified] => 2016-11-03 05:29:11 [__fuel_module__] => epssetting [__fuel_module_uri__] => epssetting [__fuel_id__] => 1 [__fuel_inline_action__] => edit [__fuel_inline__] => 0 [fuel_inline] => 0 ) Array ( [id] => 1 [last_modified] => 2016-11-03 05:31:32 )
-------------------------------------------------------------
please help me to find the issue . how should i make it work ???

Comments

  • edited November 2016
    now after making a little alteration removing unset($values['data']) ;
    it is also hitting on_before_post their i have altered the data :

    public function on_before_post($values = array())
    {
    $data=array();
    foreach($values as $key=>$val)
    {
    if(in_array($key, $this->data_filds))
    {
    $data[$key]=$val;
    }
    }
    $values['data']= json_encode($data);
    $values = parent::on_before_post($values);
    print_r($values);
    return $values;
    }

    resulting :

    Array
    (
    [fuel_other_items] =>
    [fuel_restore_version] =>
    [fuel_restore_ref_id] => 1
    [theam] => 0
    [contact_email] =>
    [id] => 1
    [data] => {"theam":"0","contact_email":""}
    [last_modified] => 2016-11-03 05:29:11
    [__fuel_module__] => epssetting
    [__fuel_module_uri__] => epssetting
    [__fuel_id__] => 1
    [__fuel_inline_action__] => edit
    [__fuel_inline__] => 0
    [fuel_inline] => 0
    )
    but again in on_before_save function it is



    Array
    (
    [id] => 1
    [data] =>
    [last_modified] => 2016-11-03 06:36:06
    )
  • edited 4:15AM
    The $values array will only contain the data necessary for the model to save. If you want all the data in the post, grab it from the method 'normalize_save_values()':
    public function on_before_save($values) { print_r($this->normalize_save_values()); return $values; }
  • edited 4:15AM
    thanks it worked ...
Sign In or Register to comment.