【发布时间】:2015-10-27 14:53:58
【问题描述】:
假设我尝试保存以下数据并且 Songs 模型的 name 属性设置了 Phalcon\Mvc\Model\Validator\PresenceOf 验证器
// Get an existing artist
$artist = Artists::findFirst('name = "Shinichi Osawa"');
// Create an album
$album = new Albums();
$album->name = 'The One';
$album->artist = $artist;
$songs = array();
// Create a first song
$songs[0] = new Songs();
$songs[0]->name = 'Star Guitar';
$songs[0]->duration = '5:54';
// Create a second song
$songs[1] = new Songs();
$songs[1]->name = '';
$songs[1]->duration = '4:29';
// Assign the songs array
$album->songs = $songs;
// Save the album + its songs
if (!$album->save()) {
foreach ($album->getMessages() as $message) {
$message->getModel(); // this returns null since model is new
}
}
我将收到一条错误消息,提示“名称”是必需的。
问题:是否有内置方法来获取发生错误的相关模型(在本例中为 $songs[1]),或者至少是错误模型的别名/索引(在本例中为歌曲/1)?
【问题讨论】:
-
$songs[1]->name = '';可能是这个。
标签: php validation error-handling phalcon