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

xdnvmnnf  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(188)

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

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()上创建一个新的Provinces and Countries条目,并使用类似的实现,而不会出现错误Object of class Town could not be converted to string
请指教,谢谢。

k5hmc34c

k5hmc34c1#

在这一行

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

相关问题