当前位置:首页 > CMS教程 > 其它CMS > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-01-22 13:43:10 浏览: 评论:0 

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

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

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

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

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

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

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

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

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

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

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

Tags: Yii自定义数据库

分享到: