Saving Multi-File type asset

edited September 2015 in Modules
Hey again,

I used
$fields['history_img'] = array('type' => 'file','overwrite' => TRUE,'display_overwrite' => FALSE, 'accept' => 'gif|jpg|jpeg|png','subfolder' => $path,'remove_subfolder' => TRUE,'display_preview' => TRUE,'is_image' => TRUE,'img_container_styles' => 'border: 2px solid #888','img_styles' => 'height: 80px','multiple' => TRUE,'style' => 'width:900px;','order' => '16');

to store path/name of multiple images in the db in the column "history_img" which has a TEXT datatype.

When i save the data - the data which i get in on_before_save is already serialized/JSONed string. So it saves a serialized data in the db. Which is ideal.

I have added the field name in $serialized_fields array at the top of the model class(students_images_model).

However when i do the following from the other model i.e. students_model,
$post = $CI->students_images_model->save($posted);

The issue i am facing is:-
The data posted is a simple string. And it does not get serialized. IT saves a string instead of a json serialized form.

I checked the $values which we get when we save/update the record --
Inside students_images_model the data is already serialized while in students_images model the data comes in string form.

Can you help understand this issue??\
Because if i save data from students_images_model it saves in correct form.
While when i save the data from students_model (i have included the fields from students_images_model to get all data in one place) - i get it in string form.

Comments

  • edited 6:16AM
    Is the data being sent in the $posted variable an array? The MY_Model::serialize_field_values() method will only serialize array data.
  • edited 6:16AM
    No it is not.
    It is coming in string format in $posted variable.
    And that is looking odd to me. Because in both the model the data is coming in the string format.
    However, when i use the students_images_model :- the data in $posted is already serialized. Is it because of the Ajax post of the data?

    (All the models here in discussion has fuel as its parent class)
  • edited 6:16AM
    What does your table look like and associated model's form_fields method look like?
  • edited September 2015
    table is id int(11) NO PRI student_id int(11) NO history_count int(11) NO 0 culture_count int(11) NO 0 family_count int(11) NO 0 history_img text YES culture_img text YES family_img text YES
    The associated modeli.e. students_images_model looks like public $serialized_fields = array('history_img','culture_img','family_img'); public function form_fields($values = array(), $related = array()) { $fields = parent::form_fields($values, $related); $path = 'igaatha/cc/'.$values['state_id'].'/'.$values['student_id'].'/'; if(!is_dir('assets/images/'.$path)) { mkdir('assets/images/'.$path,0777,TRUE); } $fields['history_img'] = array('type' => 'file','overwrite' => TRUE,'display_overwrite' => FALSE, 'accept' => 'gif|jpg|jpeg|png','subfolder' => $path,'remove_subfolder' => TRUE,'display_preview' => TRUE,'is_image' => TRUE,'img_container_styles' => 'border: 2px solid #888','img_styles' => 'height: 80px','multiple' => TRUE,'style' => 'width:900px;','order' => '16'); $fields['culture_img'] = array('type' => 'file','overwrite' => TRUE,'display_overwrite' => FALSE, 'accept' => 'gif|jpg|jpeg|png','subfolder' => $path,'remove_subfolder' => TRUE,'display_preview' => TRUE,'is_image' => TRUE,'img_container_styles' => 'border: 2px solid #888','img_styles' => 'height: 80px','multiple' => TRUE,'style' => 'width:900px;','order' => '24'); $fields['family_img'] = array('type' => 'file','overwrite' => TRUE,'display_overwrite' => FALSE, 'accept' => 'gif|jpg|jpeg|png','subfolder' => $path,'remove_subfolder' => TRUE,'display_preview' => TRUE,'is_image' => TRUE,'img_container_styles' => 'border: 2px solid #888','img_styles' => 'height: 80px','multiple' => TRUE,'style' => 'width:900px;','order' => '26'); return $fields; } public function on_before_save($values) { $values = parent::on_before_save($values); if(!empty($values['history_img']) && $values['history_img'] != '[]'){ $values['history_count'] = substr_count($values['history_img'], ",") +1; }else{ $values['history_count'] = 0; } if(!empty($values['culture_img']) && $values['culture_img'] != '[]'){ $values['culture_count'] = substr_count($values['culture_img'], ",") +1; }else{ $values['culture_count'] = 0; } if(!empty($values['family_img']) && $values['family_img'] != '[]'){ $values['family_count'] = substr_count($values['family_img'], ",") +1; }else{ $values['family_count'] = 0; } return $values; }

    The *_count saves the no. of images in the *_img column.
  • edited 6:16AM
    Are you wanting to have multiple history_img fields so you can save an array of images as JSON? If so, you'll need to convert the saved valued into an array syntax perhaps by using something like the "template" field type which will create a nested array on post of
    $_POST['history_img'][0]['image']
    $_POST['history_img'][1]['image']
    http://docs.getfuelcms.com/general/forms#template

    Or you can turn it into an array in your on_before_save method.
  • edited 6:16AM
    No, I do not want multiple history_img fields. I wanted a history_img field which saves an array of images in json format.

    I will try the second option. However, the behavior of the "file" type field was looking weird if used in associative form in other modules.
    Because at times the history_image field comes already in json string format in the on_before_save hook ["istu\/cc\/1\/16\/history_1.jpg","istu\/cc\/1\/16\/history_2.jpg","istu\/cc\/1\/16\/history_3.jpg"]
    & if used in associative form it comes in simple string format.
    istu/cc/2/1/history_1.jpg, istu/cc/2/1/history_2.jpg, istu/cc/2/1/history_3.jpg
  • edited September 2015
    OK... I think I finally understand. You have a single image field that is set to multiple which appends images with a comma delimiter it looks like. In this situation, I think it has to do with the fact that the post_process method on that field doesn't get run because it belongs to another model. One way around this would be to add a check in your Students_images_model::on_before_save() method to do something like the following:
    public function on_before_save($values) { if (!is_array($values['history_img'])) { $values['history_img'] = preg_split('#\s*,\s*|\n#', $$values['history_img']); } //..... your other code return $values; }
  • edited 6:16AM
    Yeah, as per your earlier suggestion - i did similar change to the on_before_save.
    Thanks for your help. :)

    I thought it might be a glitch in the flow & you would like to see to it for future development of FUEL CMS
Sign In or Register to comment.