phalcon中的数据库操作类Phalcon\Db,Phalcon\Db\Adapter\Pdo, Phalcon\Db\Adapter\Pdo\Mysql。这几个我看了,都没有数据库执行出错后获取相应错误的方法,不知道是怎么回事,有没有知道的啊?
假设你已经注册”falsh”服务:
php
/**
1. Register the flash service with custom CSS classes
*/
$di->set('flash', function() {
$flash = new \Phalcon\Flash\Direct(array(
'error' => 'alert alert-danger callout callout-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
));
return $flash;
});
以\Phalcon\Mvc\Model为例,在controller里面:
php
if ($Article->save()) {
$this->flash->success($Article->title . 'has been updated');
} else {
foreach ($Article->getMessages() as $message) {
$this->flashSession->error($message);
}
return $this->response->redirect($paginateUrl);
$this->view->disable();
}
当然,你也可以自定义验证信息:
php
public function validation()
{
$this->validate(new Uniqueness(array(
'field' => 'slug',
'message' => 'The slug already exists in other articles,please modify the article title'
)));
if ($this->validationHasFailed() == true) {
return false;
}
}
如果是working with models则
$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";
foreach ($robot->getMessages() as $message) {
echo $message, "\n";
}
} else {
echo "Great, a new robot was saved successfully!";
}
如果是phql则
$phql = "INSERT INTO Cars VALUES (NULL, 'Nissan Versa', 7, 9999.00, 2012, 'Sedan')";
$result = $manager->executeQuery($phql);
if ($result->success() == false)
{
foreach ($result->getMessages() as $message)
{
echo $message->getMessage();
}
}