1
votes

I am dealing with a wierd problem. I have the Config model, together with these associations:

var $belongsTo = array(
    'Language' => array(
        'className' => 'Language',
        'foreignKey' => 'default_language',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Webmaster' => array(
        'className' => 'User',
        'foreignKey' => 'webmaster',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Group' => array(
        'className' => 'Group',
        'foreignKey' => 'default_group',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
);

In my ConfigsController I have the edit() action:

function edit($id = null) {
    if (!$id && empty($this->data)) {
        $this->Session->setFlash(__('Invalid config', true));
        $this->redirect(array('action' => 'index'));
    }
    if (!empty($this->data)) {
        if ($this->Config->save($this->data)) {
            $this->Session->setFlash(__('The config has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The config could not be saved. Please, try again.', true));
        }
    }
    if (empty($this->data)) {
        $this->data = $this->Config->read(null, $id);
    }
    $languages = $this->Config->Language->find('list');
    $webmasters = $this->Config->Webmaster->find('list');
    $groups = $this->Config->Group->find('list');
    $this->set(compact('languages', 'groups', 'webmasters'));
}

And in my edit.ctp view, I have:

echo $this->Form->input('Language');
echo $this->Form->input('Webmaster');
echo $this->Form->input('Group');

Now, here is the problem: The group is correctly selected, but the other two (webmaster and language) are reseted on the first option, have no selected attribute, and as a "bonus", the language select displays this error: Notice (8): Uninitialized string offset: 0 [CORE/cake/libs/view/helper.php, line 859]

I struggled for almost 2 hours and couldn't find the isuue for this wierd problem. Could you please help me? Thank you!

EDIT

This is what the debugger shows:

Helper::__selectedArray() - CORE/cake/libs/view/helper.php, line 859

Helper::value() - CORE/cake/libs/view/helper.php, line 712 Helper::_initInputField() - CORE/cake/libs/view/helper.php, line 753 FormHelper::_initInputField() - CORE/cake/libs/view/helpers/form.php, line 2182 FormHelper::select() - CORE/cake/libs/view/helpers/form.php, line 1438 FormHelper::input() - CORE/cake/libs/view/helpers/form.php, line 882 include - APP/views/configs/edit.ctp, line 10 View::_render() - CORE/cake/libs/view/view.php, line 723 View::render() - CORE/cake/libs/view/view.php, line 419 Controller::render() - CORE/cake/libs/controller/controller.php, line 913 Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 207 Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171 [main] - APP/webroot/index.php, line 83

And in the 'code' tab:

 if (!empty($data)) {
        foreach ($data as $var) {
            $array[$var[$key]] = $var[$key];
1
Give us the stack trace of your error. (Click on "Notice (8)" to open it up > Then click on "Code") That might even help you find the solution.RabidFire
I have edited the question. I tried to figure out what is the problem from what the debugger was showing, but I couldn't.linkyndy
Could you show us line 10 of configs/edit.ctp?RabidFire
Before I changed to the solution you gave, I had echo $this->Form->input('Language');linkyndy

1 Answers

1
votes

You can use the following piece of code in your view instead:

echo $this->Form->input('default_language', array('options' => $languages));
echo $this->Form->input('webmaster', array('options' => $webmasters));
echo $this->Form->input('default_group', array('options' => $groups));

This would create a "select" drop down for each, and associate them correctly with the relevant fields.