Cakephp 2.10.24 上 Town 类的对象无法转换为字符串

问题描述 投票:0回答:1

我有这个程序,我从 cakephp 2.2 升级到 cakephp 2.10.24 以支持 php7.4。当我尝试添加新的城镇条目时,出现以下错误:

Object of class Town could not be converted to string
Error: An Internal Error Has Occurred.

Stack Trace
APP/Controller/TownsController.php line 61 → CommonComponent->_saveEntity()
            $this->Town->set($data);
            
            if ($this->Town->validates()) {
                $this->Common->_saveEntity($data, 'Town', "Town {$data['Town']['name']} was created.");
                return $this->redirect("/admin/towns");
[internal function] → TownsController->admin_add()
CORE/Cake/Controller/Controller.php line 499 → ReflectionMethod->invokeArgs()
CORE/Cake/Routing/Dispatcher.php line 193 → Controller->invokeAction()
CORE/Cake/Routing/Dispatcher.php line 167 → Dispatcher->_invoke()
APP/webroot/index.php line 92 → Dispatcher->dispatch()

TownsController.php

    function admin_add() {
                
        if ($this->Common->_isRequest('post put') && ($data = $this->request->data)) {
            $this->Town->set($data);
            
            if ($this->Town->validates()) {
                $this->Common->_saveEntity($data, 'Town', "Town {$data['Town']['name']} was created."); //line 61
                return $this->redirect("/admin/towns");
            }
        }
    }

CommonComponent.php

    function _saveEntity($data=array(), $modelClass="", $flash_message=null) {
        if (empty($modelClass) && isset($this->controller->modelClass)) {
            $modelClass = $this->controller->modelClass;
        }
        if (empty($modelClass)) {
            $keys = array_keys($data);
            $modelClass = $keys[0];
        }
        $model =& $this->_getEntity($modelClass);

        try {
            $model->id = null;
            // $model->recursive = -1;
            $saved = $model->save($data, false);
        } catch(Exception $e) {
            if ($flash_message !== false) {
                $this->Session->setFlash("Unable to save $model.", 'default', array('class' => 'error'));
            }
            return false;
        }
        if ($flash_message === null) {
            $op = Hash::check($data, "$modelClass.id") ? 'updated' : 'created';
            $flash_message = "$modelClass {". $modelClass . ".title} was $op.";
        }
        if ($flash_message) {
            $this->_extractAndReplace($flash_message);
            $this->Session->setFlash($flash_message, 'default', array('class' => 'success'));
        }
        return $saved;
    }

但是,我可以为省和国家/地区创建一个新条目,并在

TownsController.php admin_add()
上找到类似的实现,而不会出现错误
Object of class Town could not be converted to string

请多多指教,谢谢。

php cakephp
1个回答
0
投票

在这一行

$this->Session->setFlash("Unable to save $model.", 'default', array('class' => 'error'));

您正在使用

$model
变量(它应该是一个对象)作为字符串。这会在 PHP 中引发错误。

根据您的代码,很可能您需要使用

$modelClass

$this->Session->setFlash("Unable to save $modelClass.", 'default', array('class' => 'error'));
© www.soinside.com 2019 - 2024. All rights reserved.