当我试图保存我的模型if( !$myModel->save() )
时,如果我想获取错误消息,我必须环绕尝试捕捉我的状况...我无法检查是否save()为false,因为这不会返回错误。
如何在不使用try catch的情况下获取$myModels->getMessages()
?
我想这样做example并返回json_encode,如果保存不起作用。
答案 0 :(得分:0)
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!";
}