Yii2常用操作

获取添加或修改成功之后的数据id

$insert_id = $UserModel->attributes['id'];

执行原生sql

$list = Yii::$app->db->createCommand('select * from user')->queryAll();

获取执行后的sql

$sql = UserModel::find()->createCommand()->getRawSql();

获取到$model->save后的报错信息

Model

public static function getModelError($model)
{
    $errors = $model->getErrors();    //得到所有的错误信息
    if (!is_array($errors)) {
        return '';
    }
    $firstError = array_shift($errors);
    if (!is_array($firstError)) {
        return '';  
    }
    return array_shift($firstError);
}   

Controller

throw new \RuntimeException('保存失败:'.$model::getModelError($model));

开发常用知识点总结:

https://www.yiichina.com/topic/8274

了解更多:

https://www.yiichina.com/doc/guide/2.0

https://www.yiiframework.com/doc/guide/2.0/zh-cn

猜你喜欢

转载自www.cnblogs.com/yulongcode/p/11628348.html
今日推荐