【问题标题】:Populate Fields From one Controller, in another.. CAKEPHP从一个控制器填充字段,在另一个.. CAKEPHP
【发布时间】:2013-01-21 17:48:42
【问题描述】:

我目前正在尝试允许用户存档已完成的事件。

然后该事件将在存档表中查看。

所以基本上我有一个存档表和一个事件表,当用户想要存档事件时,他们应该能够在存档添加表单中查看事件(需要由 $id 填充事件)。

但我不知道如何填充该字段..我尝试设置一个值..但事件不是会话,因此不起作用,我还尝试在开始时设置 $id表格,但这也没有用。

这是我在事件控制器中存档功能的代码。

   public function archive($id = null) {
        if ($this->request->is('post')) {
            $event = $this->Event->read($id);
            $archive['Archive'] = $event['Event'];
            $archive['Archive']['eventID'] = $archive['Archive']['archiveID'];
            unset($archive['Archive']['archiveID']);
            $this->loadModel('Archive');
            $this->Archive->create();
            if ($this->Archive->save($archive)) {
                $this->Session->setFlash(__('The event has been archived'));
                $this->Event->delete($id);
                $this->redirect(array('action' => 'eventmanage'));
            } else {
                $this->Session->setFlash(__('The event could not be archived. Please, contact the administrator.'));
            }
        }
    }

【问题讨论】:

    标签: php forms cakephp cakephp-2.1


    【解决方案1】:

    您需要执行以下操作之一:

    在控制器中使用$this->request->data 设置字段的值。

    public function add($id = null) {
        if ($this->request->is('post')) {
            [..snip..]
        }
        $this->loadModel('Event');
        $event = $this->Event->read($id);
        $this->request->data['Archive'] = $event['Event'];
    }
    

    更新表单以设置值。

    使用相同的事件读取更新现有代码:

    public function add($id = null) {
        if ($this->request->is('post')) {
            [..snip..]
        }
        $this->loadModel('Event');
        $this->set('event', $this->Event->read($id));
    }
    

    然后在 Archives/add.ctp 文件中的表单中,更新每个输入以反映 $event 的值。

    echo $this->Form->input('eventID', array('type' => 'hidden', 'value' => $event['Event']['id']));
    

    编写一个移动记录的函数。

    在事件视图上放置一个名为“存档”的按钮。在事件控制器中创建一个方法来归档事件。

    public function archive($id = null) {
        if ($this->request->is('post')) {
            $event = $this->Event->read($id);
            $archive['Archive'] = $event['Event'];
            $archive['Archive']['event_id'] = $archive['Archive']['id'];
            unset($archive['Archive']['id']);
            $this->loadModel('Archive');
            $this->Archive->create();
            if ($this->Archive->save($archive)) {
                $this->Session->setFlash(__('The event has been archived'));
                $this->Event->delete($id);
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The event could not be archived. Please, contact the administrator.'));
            }
        }
    }
    

    【讨论】:

    • 你好 Chuck,你的第二个选项,在哪个页面,'$this->set('event', $this->Event->read($id));'去吗?
    • 感谢您的帮助 Chuck,我现在遇到一个错误,“未定义变量:事件”我也会更新我上面的代码,这样您就可以看到我做了什么
    • 您可能需要更新您的阵列:$event['id']$event['Event']['id']。此外,您需要将$this->set('event'.. 移到if ($post) 之外,否则它将永远无法设置。
    • 我现在收到以下错误:“致命错误:在 /homepages/3/d439567456/htdocs/cakephp/app/Controller/ArchivesController 中的非对象上调用成员函数 read()。第 38 行的 php' 有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2010-10-10
    • 1970-01-01
    • 2010-10-06
    相关资源
    最近更新 更多