当前位置:Gxlcms > PHP教程 > php的phalcon框架的db怎样获取mysql执行过程中的错误信息?

php的phalcon框架的db怎样获取mysql执行过程中的错误信息?

时间:2021-07-01 10:21:17 帮助过:42人阅读

phalcon中的数据库操作类Phalcon\Db,Phalcon\Db\Adapter\Pdo, Phalcon\Db\Adapter\Pdo\Mysql。这几个我看了,都没有数据库执行出错后获取相应错误的方法,不知道是怎么回事,有没有知道的啊?

回复内容:

phalcon中的数据库操作类Phalcon\Db,Phalcon\Db\Adapter\Pdo, Phalcon\Db\Adapter\Pdo\Mysql。这几个我看了,都没有数据库执行出错后获取相应错误的方法,不知道是怎么回事,有没有知道的啊?

  1. 假设你已经注册"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;
});
  1. 以\Phalcon\Mvc\Model为例,在controller里面:
phpif ($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();

                    }
  1. 当然,你也可以自定义验证信息:
phppublic 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();
    }
}

人气教程排行