0

我无法找出我的函数出错的地方,我想知道是否有任何方法让我找出函数在使用 net beans 或在执行函数时中断的地方。

如果您想查看该功能,那么这里是它的副本:

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

so far i have tried using the `$errors = $this->Archive->validationErrors;` function but all i receive back is the word 'array' 

更新

我已将事件保存为存档,但它不会发送消息$this->Session->setFlash(__('The event has been archived'));并且它不会重定向回 eventmanage,它会创建一个指向 /Controller/Action/id 的新链接,例如 /events/archive/14 。

谢谢

4

2 回答 2

4

validationErrors属性返回一个数组。您不能在 PHP 中回显数组对象,因为它是数据的集合,而不是可以“只是”输出到屏幕的东西。不过还有其他函数可以显示完整数组,例如var_dump()print_r()或 CakePHP 方法debug()Debugger::dump()。所以尝试这样的事情:

$errors = $this->Archive->validationErrors;
if (!empty($errors)) { debug($errors); }

您还可以使用implode()将所有验证错误作为字符串输出到您的 flash 消息中,例如:

$this->Session->setFlash(__('The event could not be archived, because of the following errors: ' . implode('<br/>', $errors) . '. Please, contact the administrator.'));

这将显示所有错误,并在 Flash 消息中以中断分隔。

更新 关于您最近的问题更新,未设置闪信,因为您在设置之前重定向。因此,该声明永远无法达成。在你的控制器中交换 setFlash 和 redirect 方法,你会得到:

$this->Session->setFlash(__('The event has been archived'));
$this->redirect(array('action' => 'eventmanage'));
于 2013-01-28T16:37:52.577 回答
0

我很难理解需要为

$archive['Archive']['archiveID'] = $event['Event']['eventID'];

并在之后立即取消设置

unset($archive['Archive']['archiveID']);

如果在您的 Db 'archiveID' 中是必需的值,那么如果该值不存在,我认为 MySQL 不会接受任何数据。

您必须检查表的结构。

于 2013-01-28T18:59:52.913 回答