首页 > 数据库 >yii2数据库访问对象

yii2数据库访问对象

时间:2024-03-30 14:33:17浏览次数:26  
标签:execute xhj 数据库 db 访问 createCommand yii2 id SELECT

yii2数据库访问对象

配置数据库链接

$db = new yii\db\Connection([
            'dsn' => 'mysql:host=localhost;dbname=xhj',
            'username' => 'root',
            'password' => '123456',
            'charset' => 'utf8',
        ]);

或 配置文件 config/web.php 添加

 'components' => [
        // ...
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=example',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],

查询

        //$user = Yii::$app->db->createCommand('SELECT * FROM country')->queryAll();
        
        $result = $db->createCommand('SELECT * FROM xhj_backend_member')->queryAll();
        $result = $db->createCommand('SELECT * FROM xhj_backend_member where id= 8')->queryOne();
        $result = $db->createCommand('SELECT username FROM xhj_backend_member')->queryColumn();
        $result = $db->createCommand('SELECT COUNT(*) FROM xhj_backend_member')->queryScalar();

        $id = 8;
        $result = $db->createCommand('SELECT * FROM xhj_backend_member where id=:id')->bindValue(':id',$id)->queryOne();

        $params = [':id' => 1, ':status' => 1];
        $result = $db->createCommand('SELECT * FROM xhj_backend_member where id=:id AND status=:status')->bindValues($params)->queryOne();
        $result = $db->createCommand('SELECT * FROM xhj_backend_member where id=:id AND status=:status',$params)->queryOne();

CURD

        // Yii::$app->db->createCommand('UPDATE country SET population=112233 WHERE `code`="US"')->execute();

       // Yii::$app->db->createCommand()->insert('country', ['code' => 'AD', 'name' => '艾莉','population'=>2])->execute();
       // Yii::$app->db->createCommand()->update('country', ['population' => 222], ['code'=>'AD'])->execute();

        //Yii::$app->db->createCommand()->delete('country', ['code'=>'AD'])->execute();

        Yii::$app->db->createCommand()->batchInsert('country', ['code','name','population'],[['code1', 'name1', 1], ['code2', 'name2', 2],['code3', 'name3', 3]])->execute();

事务

        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            $db->createCommand($sql1)->execute();
            $db->createCommand($sql2)->execute();
            // ... executing other SQL statements ...

            $transaction->commit();
        } catch(\Exception $e) {
            $transaction->rollBack();
            throw $e;
        } catch(\Throwable $e) {
            $transaction->rollBack();
            throw $e;
        }

标签:execute,xhj,数据库,db,访问,createCommand,yii2,id,SELECT
From: https://www.cnblogs.com/hu308830232/p/18105448

相关文章

  • yii2 扩展
    yii2扩展示例安装扩展composerrequire--prefer-distyiisoft/yii2-imagine使用扩展useyii\imagine\Image;publicfunctionactionIndex(){Image::thumbnail('@webroot/img/test.jpg',120,120)->save(Yii::getAlias('@runt......
  • yii2表单使用
    yii2表单使用模型models/form/EntryForm.php<?phpnamespaceapp\models\form;useyii\base\Model;classEntryFormextendsModel{public$name;public$email;publicfunctionrules(){return[[['name',&......
  • yii2 rules验证规则大全
    yii2rules验证规则大全required : 必须值验证属性[['name','email'],'required'][['name'],'required','message'=>'提示信息']email : 邮箱验证[['email'],'email','message'=......
  • Yii2行为用法
    Yii2行为用法使用行为(behavior)可以在不修改现有类的情况下,对类的功能进行扩充行为类(app\common\behaviors\MyBehavior)<?phpnamespaceapp\common\behaviors;useyii\base\Behavior;classMyBehaviorextendsBehavior{public$name;public$age;......
  • yii2模块
    yii2模块模块是独立的软件单元,由模型, 视图, 控制器和其他支持组件组成, 终端用户可以访问在应用主体中已安装的模块的控制器, 模块被当成小应用主体来看待,和应用主体不同的是, 模块不能单独部署,必须属于某个应用主体。模块文件结构modulesadmincontrollers......
  • yii2服务定位器
    yii2服务定位器服务定位器是注册和访问组件的对象注册组件useyii\di\ServiceLocator;useyii\caching\FileCache;$locator=newServiceLocator;//通过一个可用于创建该组件的类名,注册"cache"(缓存)组件。$locator->set('cache','yii\caching\ApcCache');//通过......
  • yii2响应(Responses)
    yii2响应(Responses)状态码Yii::$app->response->statusCode=200;异常yii\web\BadRequestHttpException:statuscode400.yii\web\ConflictHttpException:statuscode409.yii\web\ForbiddenHttpException:statuscode403.yii\web\GoneHttpException:......
  • yii2过滤器
    yii2过滤器过滤器是控制器动作执行之前或之后执行的对象。示例过滤器common\components\ActionTimeFilter<?phpnamespaceapp\common\components;useYii;useyii\base\ActionFilter;classActionTimeFilterextendsActionFilter{private$_startTime;......
  • yii2 资源
    yii2资源AppAsset.php<?phpnamespaceapp\assets;useyii\web\AssetBundle;classAppAssetextendsAssetBundle{public$basePath='@webroot';public$baseUrl='@web';public$css=['css/site.css�......
  • yii2请求组件
    yii2请求组件应用的请求是用yii\web\Request对象来表示的请求参数$request=Yii::$app->request;$get=$request->get();//等价于:$get=$_GET;$id=$request->get('id');//等价于:$id=isset($_GET['id'])?$_GET['id']:null;$i......