我有一个名为ValuesHelper
的模型助手,具有以下代码片段:
<?php
namespace common'models;
class ValueHelpers
{
/**
* return the value of status by the its name string
* example: 'Active'
* @param string $status_name
*/
public static function getStatusValue($sta8tus_name)
{
$connection = 'Yii::$app->db;
$sql = "SELECT id FROM status WHERE status_name=:status_name";
$command = $connection->createCommand($sql);
$command->bindValue(':status_name', $status_name);
// the issue in the next line
$result = $command->queryOne() or die($connection->getFirstError()."error");
return $result['id'];
}
}
我不知道如何在or die()
子句中实现像mysql_error()
。我尝试了or die ($command->getFirstError())
但也失败了。顺便说一下,我故意设置了错误的参数名称$sta8tus
以格式化用于创建错误的环境。
为什么不看看原木而不是在那里放一个骰子呢?
另请参阅此处: http://www.yiiframework.com/doc-2.0/yii-db-command.html#queryOne()-detail如果命令有问题,则会引发异常。您必须捕获该异常并使用它,这是最佳实践。
所以做一个
try {
$result = $command->queryOne();
} catch (yii'db'Exception $e) {
do your stuff here
}