我有一个程序,我从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
。
请指教,谢谢。
1条答案
按热度按时间k5hmc34c1#
在这一行
字符串
您正在使用
$model
变量-这应该是一个对象-作为一个字符串。这在PHP中抛出一个错误。根据您的代码,很可能需要使用
$modelClass
型