Yii框架自定义数据库操作组件示例

吾爱主题 阅读:155 2021-09-16 16:06:00 评论:0

本文实例讲述了Yii框架自定义数据库操作组件。分享给大家供大家参考,具体如下:

Yii 的数据库操作对象提供的方法确实很方便。 但是有的时候我们已经习惯了我们以前编写php的数据库操作语法,没有那么多时间去仔细看每个Yii提供的数据库操作语法,怎么办呢? 那就是一边学习,一边二次封装自己习惯的数据库操作类。 以后我们使用数据库操作对象,就用我们自己定义的组件去操作。

将我的数据库操作组件注册进配置文件web.php 中

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 array (    'components' => array (      //自定义数据库操作组件      'dbOper'  => array (        'class'   => 'app\components\DbOper\realization\DbRealization1'      ),      //Yii 框架数据库连接组件      'db'      =>  array (          'class'     => 'yii\db\Connection' ,          'dsn'      => 'mysql:host=localhost;dbname=yii' ,          'username'   => 'root' ,          'password'   => '123456' ,          'charset'    => 'utf8'        );    ) )

然后我们就可以在components 目录下定义我们的数据库操作类了。 因为,不知道怎么去获得php pdo 的原生操作对象,所以这里是对Yii数据库操作类的一个二次封装。

接口文件 DbOper.php 自定义的数据库操作类都得实现该接口

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 <?php namespace app\components\DbOper; /**   * 自定义数据库操作组件 依赖系统定义组件db   */ interface DbOper {    /**     * 查询多条数据     * @param     * String $sql 需要查询的sql语句     * array $keyVal 字段映射     * @return     * array 查询结果     */    public function fetchAll( $sql = '' , $keyVal = array ());    /**     * 查询一条数据 原生sql     * @param     * String $sql 需要查询的sql语句     * array $keyVal 字段映射     * @return     * array 查询结果     */    public function fetch( $sql = '' , $keyVal = array ());    /**     * 添加数据     * @param     * String $tableName 表名     * array $values 要插入的数据     * @return     * int 受影响的行数     */    public function insert( $tableName = '' , $values = array ());    /**     * 更新数据     * @param     * String $tableName 表名     * array | String $where 修改条件 为 1 时更改该表所有的行     * array $update 要更新的数据     * @return     * int 受影响的行数     */    public function update( $tableName = '' , $where = '' , $update = array ());    /**     * 删除数据     * @param     * String $tableName 表名     * array | String $where 删除条件     * @return     * int 受影响的行数     */    public function delete ( $tableName = '' , $where = '' );    /**     * 事务处理     * @param     * array $sqls 要执行的sql集合     * return     * boolean 是否执行成功     */    public function transcation( $sqls = array ());    /**     * 获得数据库链接     */    public function getYiiDbConnection(); }

针对DbOper 接口的实现类 DbRealization1.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 <?php namespace app\components\DbOper\realization; use Yii; use app\components\DbOper\DbOper; /**   * 自定义数据库操作组件实现类   */ class DbRealization1 implements DbOper {    private $db = null;    /**     * interface @Override     */    public function fetchAll( $sql = '' , $keyVal = array ())    {      if ( $sql === '' )        return array ();      $result = $this ->getQueryObj( $sql , $keyVal )->queryAll();      if ( $result )        return $result ;      else        return array ();    }    /**     * interface @Override     */    public function fetch( $sql = '' , $keyVal = array ())    {      if ( $sql === '' )        return array ();      $result = $this ->getQueryObj( $sql , $keyVal )->queryOne();      if ( $result )        return $result ;      else        return array ();    }    /**     * interface @Override     */    public function insert( $tableName = '' , $values = array ())    {      if ( $tableName === '' )        return 0;      $insert = $this ->getYiiDbConnection()->createCommand();      if ( is_array ( $values [0]))      {        $keys = array_keys ( $values [0]);        return $insert ->batchInsert( $tableName , $keys , $values )->execute();      }      return $insert ->insert( $tableName , $values )->execute();    }    /**     * interface @Override     */    public function update( $tableName = '' , $where = '' , $update = array ())    {      if ( $tableName === '' )        return 0;      if ( $where === '' )        return 0;      return $this ->getYiiDbConnection()          ->createCommand()          ->update( $tableName , $update , $where )          ->execute();    }    /**     * interface @Override     */    public function delete ( $tableName = '' , $where = '' )    {      if ( $tableName === '' )        return 0;      return $this ->getYiiDbConnection()          ->createCommand()          -> delete ( $tableName , $where )          ->execute();    }    /**     * 获得查询操作对象     * @return     * Object     */    private function getQueryObj( $sql = '' , $keyVal = array ())    {      $query = $this ->getYiiDbConnection()->createCommand( $sql );      if (! empty ( $keyVal ))        $query ->bindValues( $keyVal );      return $query ;    }    /**     * interface @Override     */    public function transcation( $sqls = array ())    {      if ( empty ( $sqls ))        return false;      $db = $this ->getYiiDbConnection();      $outerTransaction = $db ->beginTransaction();      $runClient = true;      try      {        foreach ( $sqls as $sql )        {          $db ->createCommand( $sql )->execute();        }        $outerTransaction ->commit();      } catch (\Exception $e ){        $runClient = false;        $outerTransaction ->rollback();      }      return $runClient ;    }    /**     * interface @Override     */    public function getYiiDbConnection()    {      if ( $this ->db === null)      {        $this ->db = Yii:: $app ->db;      }      return $this ->db;    } }

注意:我的自定义数据库操作类 依赖 Yii::$app->db 这个组件, 也就是框架自带的数据库连接组件

然后我们就可以通过 Yii::$app->dbOper 去操作数据库了。

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/u014559227/article/details/77507808

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容