CakePHP:未初始化的字符串偏移量:视图中为 0

发布于 2024-10-07 13:43:53 字数 3206 浏览 2 评论 0原文

我正在处理一个奇怪的问题。 我有 Config 模型以及这些关联:

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' => ''
    ),
);

在我的 ConfigsController 中,我有 edit() 操作:

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'));
}

在我的 edit 中.ctp 视图,我有:

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

现在,问题是:组已正确选择,但其他两个(网站管理员和语言)在第一个选项上重置,有 no < code>selected 属性,作为“奖励”,语言选择显示此错误: Notice (8): Uninitialized string offset: 0 [CORE/cake/libs/view/helper.php, line 859]

我苦苦挣扎了近 2 个小时,找不到这个奇怪问题的问题。请你帮助我好吗?谢谢你!

编辑

这是调试器显示的内容:

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

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

在“代码”选项卡中:

 if (!empty($data)) {
        foreach ($data as $var) {
            $array[$var[$key]] = $var[$key];

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仲春光 2024-10-14 13:43:53

您可以在视图中使用以下代码段:

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));

这将为每个创建一个“选择”下拉列表,并将它们与相关字段正确关联。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文