Saving Multi-File type asset
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
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)
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.
$_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.
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
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; }
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