2

I have a model helper called ValuesHelper in common/models with the following code snippet:

 <?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'];
        }
    }

I don't know how to implement like mysql_error() in or die() clause. I tried or die ($command->getFirstError()) but it failed too. By the way, on purpose, I set wrong parameter name $sta8tus to format an environment for creating the error.

1 Answer 1

2

Why not look at the logs instead of putting a die there?

Also take a look here: http://www.yiiframework.com/doc-2.0/yii-db-command.html#queryOne()-detail If the command has a problem then it throws an exception. You have to catch that exception and use it, that is the best practice.

So do a

try {
    $result = $command->queryOne();
} catch (yii\db\Exception $e) {
    do your stuff here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.