3

I have an admin form with a multiselect form field. Hitting save is passing this as an array and I would like to convert these array values to a comma delimited string before saving to the database. To be honest, I'm not sure how to access this property from my controller's save() method in order to perform the array to string conversion.

form data i want to access and modify

So far I either end up with a completely bank page, or an error message:

Error Save failed with the following error:

No error listed. I can only assume this is because I am passing an array to a DB field which is expecting a string.

This is what I have now. No doubt this is very far off the mark. I'm at the point where I'm just taking shots in the dark, hoping to get lucky.

edit: ok so this is performing the array to string conversion but doesn't seem to be setting the value before submitting to the db. still not quite there but getting closer, i think.

function save($key = null, $urlVar = null) {
    // get database and query objects
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);

    // get submitted form data
    $data = JFactory::getApplication();
    $array = $data->input->post->getArray();

    echo "<pre>";
    print_r($array);
    echo "</pre>";

    // Convert Instructor(s) field to an array after being fetched from the database
    if (is_array($array['jform']['instructors'])){
        $instructors = implode(',', $array['jform']['instructors']);
    }
    unset($array['jform']['instructors']);
    $array['jform']['instructors'] = $instructors;
    echo $array['jform']['instructors'];
    // run the parent's save function
    parent::save($key = null, $urlVar = null);
}
3

In MVC the controller is responsible for getting the data from the request and passing it into the model.

At the moment you are manipulating data from the request in your overwritten save method and then you call the parent save method (which will get fresh request data again).

A better place for your modification would be the model. There is also a save method (which gets automatically called by the controller, so you can delete your save method from your controller).

In your model class you can put this code:

public function save($data)
{
    if (is_array($data['instructors']))
    {
        $data['instructors'] = implode(',', $data['instructors']);
    }

    return parent::save($data);
}

If your data comes from a multiple select field, you might want to transform the string back to an array when you edit an item. The best place for this is the loadFormData() method:

protected function loadFormData()
{
    // Check the session for previously entered form data.
    $data = JFactory::getApplication()->getUserState('com_example.edit.item.data', array());

    if (empty($data))
    {
        $data = $this->getItem();
        $data->instructors = explode(',', $data->instructors);
    }

    return $data;
}
  • thank you. i was experiencing this problem with the save() function in my controller, because even after i had the conversion working, it was still trying to submit an array. – dustaMaphone May 8 '15 at 15:04
  • i followed your instruction, but receive this error when saving.. Save failed with the following error: CourseListTableSection::bind(NULL) – dustaMaphone May 8 '15 at 15:04
  • Did you remove your save method from your controller? Do you overwrite anything in your JTable class? Does everything work when you remove the save method from the model again (obviously except that the instructions won't be saved)? – fruppel May 8 '15 at 15:12
  • i have removed the method from my controller. the JTable class only has the constructor function __construct(&$db). – dustaMaphone May 8 '15 at 15:16
  • i had previously overwritten the bind function trying to follow a previous example.. this was still in place and i have removed this overwrite. i am now back to getting the previous error -- save failed with the following error: and then nothing listed – dustaMaphone May 8 '15 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.