【发布时间】:2016-05-09 13:21:20
【问题描述】:
当我尝试像这样if( !$myModel->save() ) 保存我的模型时,如果我想获取错误消息,我必须包围 try catch 我的条件...我无法检查 save() 是否为假,因为这不会返回错误。
如何在不使用 try catch 的情况下获取$myModels->getMessages()?
我想这样做example,如果保存不起作用,则返回 json_encode。
【问题讨论】:
当我尝试像这样if( !$myModel->save() ) 保存我的模型时,如果我想获取错误消息,我必须包围 try catch 我的条件...我无法检查 save() 是否为假,因为这不会返回错误。
如何在不使用 try catch 的情况下获取$myModels->getMessages()?
我想这样做example,如果保存不起作用,则返回 json_encode。
【问题讨论】:
The example from the docs,向您展示保存$myModel 后如何获取消息
$robot = new Robots();
$robot->type = "mechanical";
$robot->name = "Astro Boy";
$robot->year = 1952;
if ($robot->save() == false) {
echo "Umh, We can't store robots right now: \n";
// get the messages of the saved Robot.
foreach ($robot->getMessages() as $message) {
echo $message, "\n";
}
} else {
echo "Great, a new robot was saved successfully!";
}
【讨论】: