本文實(shí)例講述了Yii框架自定義數(shù)據(jù)庫操作組件。分享給大家供大家參考,具體如下:
Yii 的數(shù)據(jù)庫操作對象提供的方法確實(shí)很方便。 但是有的時候我們已經(jīng)習(xí)慣了我們以前編寫php的數(shù)據(jù)庫操作語法,沒有那么多時間去仔細(xì)看每個Yii提供的數(shù)據(jù)庫操作語法,怎么辦呢? 那就是一邊學(xué)習(xí),一邊二次封裝自己習(xí)慣的數(shù)據(jù)庫操作類。 以后我們使用數(shù)據(jù)庫操作對象,就用我們自己定義的組件去操作。
將我的數(shù)據(jù)庫操作組件注冊進(jìn)配置文件web.php 中
array( 'components' => array( //自定義數(shù)據(jù)庫操作組件 'dbOper' => array( 'class' => 'app\components\DbOper\realization\DbRealization1' ), //Yii 框架數(shù)據(jù)庫連接組件 'db' => array( 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii', 'username' => 'root', 'password' => '123456', 'charset' => 'utf8' ); ) )
然后我們就可以在components 目錄下定義我們的數(shù)據(jù)庫操作類了。 因?yàn)椋恢涝趺慈カ@得php pdo 的原生操作對象,所以這里是對Yii數(shù)據(jù)庫操作類的一個二次封裝。
接口文件 DbOper.php 自定義的數(shù)據(jù)庫操作類都得實(shí)現(xiàn)該接口
?php namespace app\components\DbOper; /** * 自定義數(shù)據(jù)庫操作組件 依賴系統(tǒng)定義組件db */ interface DbOper { /** * 查詢多條數(shù)據(jù) * @param * String $sql 需要查詢的sql語句 * array $keyVal 字段映射 * @return * array 查詢結(jié)果 */ public function fetchAll($sql='',$keyVal=array()); /** * 查詢一條數(shù)據(jù) 原生sql * @param * String $sql 需要查詢的sql語句 * array $keyVal 字段映射 * @return * array 查詢結(jié)果 */ public function fetch($sql='',$keyVal=array()); /** * 添加數(shù)據(jù) * @param * String $tableName 表名 * array $values 要插入的數(shù)據(jù) * @return * int 受影響的行數(shù) */ public function insert($tableName='',$values=array()); /** * 更新數(shù)據(jù) * @param * String $tableName 表名 * array | String $where 修改條件 為 1 時更改該表所有的行 * array $update 要更新的數(shù)據(jù) * @return * int 受影響的行數(shù) */ public function update($tableName='',$where='',$update=array()); /** * 刪除數(shù)據(jù) * @param * String $tableName 表名 * array | String $where 刪除條件 * @return * int 受影響的行數(shù) */ public function delete($tableName='',$where=''); /** * 事務(wù)處理 * @param * array $sqls 要執(zhí)行的sql集合 * return * boolean 是否執(zhí)行成功 */ public function transcation($sqls = array()); /** * 獲得數(shù)據(jù)庫鏈接 */ public function getYiiDbConnection(); }
針對DbOper 接口的實(shí)現(xiàn)類 DbRealization1.php
?php namespace app\components\DbOper\realization; use Yii; use app\components\DbOper\DbOper; /** * 自定義數(shù)據(jù)庫操作組件實(shí)現(xiàn)類 */ 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; } }
注意:我的自定義數(shù)據(jù)庫操作類 依賴 Yii::$app->db 這個組件, 也就是框架自帶的數(shù)據(jù)庫連接組件
然后我們就可以通過 Yii::$app->dbOper 去操作數(shù)據(jù)庫了。
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。
標(biāo)簽:深圳 大同 駐馬店 雙鴨山 江門 內(nèi)江 石嘴山
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Yii框架自定義數(shù)據(jù)庫操作組件示例》,本文關(guān)鍵詞 Yii,框架,自定義,數(shù)據(jù)庫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。