YII動態(tài)模型(動態(tài)表名)支持分析
本文分析了YII動態(tài)模型(動態(tài)表名)支持機制。分享給大家供大家參考,具體如下:
給YII 框架增加動態(tài)模型支持
Yii框架中的數(shù)據(jù)模型使用靜態(tài)機制,如果要使用模型方式操作某張數(shù)據(jù)表,就必須得事先創(chuàng)建數(shù)據(jù)表對應的模型類(位于 protected/models 目錄下),這種方式,在有的情況下給我們的工作帶來了一些不便,如僅僅將數(shù)據(jù)表進行顯示,或者數(shù)據(jù)表是動態(tài)生成的,或者要實現(xiàn)數(shù)據(jù)表模型中的讀寫分離,(如數(shù)據(jù)寫入與數(shù)據(jù)呈現(xiàn)邏輯可能定義到不同的模型中,以提高性能,如前后臺的分離)。
為解決這個問題,經(jīng)過我反復調試,已經(jīng)為Yii 擴展出了動態(tài)數(shù)據(jù)表模型支持,使用時簡單提供表名,即可將其當作普通的數(shù)據(jù)表模型進行操作,當然帶來的問題就是無數(shù)據(jù)驗證。即使是這樣,也給數(shù)據(jù)顯示帶來極大的方便。如果在使用的過程中有任何問題,可隨時聯(lián)系筆者信箱 zhangxugg@163.com 進行探討或索取源碼。
處理方法如下:
請將我提供的DbTable.php 放置到 protected/models/ 目錄下,然后就可以在任何位置使用之。
產(chǎn)生新記錄:
$memo = new DTable('{{memo}}'); $memo->msg = 'this is content'; $memo->save(); //last insertid echo $memo->id ;
讀取已有記錄:
$memo = DTable::model('{{memo}}')->findByPk(12); $memo->msg = "modefid content"; $memo->save(); //使用非默認數(shù)據(jù)庫,需要在 config/main.php 文件中定義數(shù)據(jù)庫連接,如: 'components' => array( 'db-other'=>array( 'class' => 'CDbConnection', 'connectionString' => 'mysql:host=localhost;dbname=cdcol;charset=utf8', 'username' => 'root', 'password' =>'', 'tablePrefix' => '', 'autoConnect' => false, ), ); DTable::$db = Yii::app()->getComponent('db-other'); $memo = DTable::model('{{memo}}')->findByPk(12);
Dynamic model supports for Yii framework 1.1.10
/** * DTable class file. * @author zhangxugg@163.com * @since Yii 1.1.10 * @package application.models * @version $Id DTable.php 1 2012-03-24 23:29 $ DTable provides dynamic table model supports for some application entironment such as dynamic-generated database tables, or simple read actions. please contact zhangxugg@163.com for the source code. new record : $model = new DTable('table_name'); //use table prefix: $model = new DTable('{{table_name}}'); $model->id = $id; $model->name = 'zhangxugg@163.com'; $model->save(); update: $model = DTable::model('{{table_name}}') $model->name = 'zhangxugg@163.com' $model->save(); $list = $model->findAll(); use non-default database connection : DTable::$db = Yii::app()->getCompoments('db-extra'); tips : you must define the database connection informations in config/main.php 'components' => array( 'db-extra' => array( 'class' => 'CDbConnection', 'connectionString' => 'mysql:host=localhost;dbname=cdcol;charset=utf8', 'username' => 'root', 'password' =>'', 'tablePrefix' => '', 'autoConnect' => false, ), ) DTable source code : class DTable extends CActiveRecord { private static $tableName ; public function __construct($table_name = '') { if($table_name === null) { parent::__construct(null); } else { self::$tableName = $table_name ; parent::__construct(); } } public static function model($table_name='') { self::$tableName = $table_name ; return parent::model(__CLASS__); } public function tableName() { return self::$tableName; } } */
更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
- YII2框架中自定義用戶認證模型,完成登陸和注冊操作示例
- Yii2.0框架模型添加/修改/刪除數(shù)據(jù)操作示例
- Yii2.0框架模型多表關聯(lián)查詢示例
- Yii中Model(模型)的創(chuàng)建及使用方法
- yii框架表單模型使用及以數(shù)組形式提交表單數(shù)據(jù)示例
- PHP YII框架開發(fā)小技巧之模型(models)中rules自定義驗證規(guī)則
- PHP的Yii框架中Model模型的學習教程
- Yii框架數(shù)據(jù)模型的驗證規(guī)則rules()被執(zhí)行的方法
- Yii框架表單模型和驗證用法
- Yii模型操作之criteria查找數(shù)據(jù)庫的方法
- Yii數(shù)據(jù)模型中rules類驗證器用法分析
- yii2.0框架多模型操作示例【添加/修改/刪除】
相關文章
thinkPHP5實現(xiàn)數(shù)據(jù)庫添加內容的方法
這篇文章主要介紹了thinkPHP5實現(xiàn)數(shù)據(jù)庫添加內容的方法,結合實例形式較為詳細的分析了thinkPHP5數(shù)據(jù)庫的配置、模型、控制器的使用及數(shù)據(jù)插入相關操作技巧,需要的朋友可以參考下2017-10-10PHP中new static()與new self()的比較
在寫代碼時發(fā)現(xiàn) new static(),覺得實例化的地方不是應該是 new self()嗎?怎么回事?通過查閱相關資料才知道具體情況,下面小編整理下方便日后查找2016-08-08Yii2搭建后臺并實現(xiàn)rbac權限控制完整實例教程
這篇文章主要介紹了Yii2搭建后臺并實現(xiàn)rbac權限控制完整實例教程的相關資料,需要的朋友可以參考下2016-04-04ThinkPHP5+Layui實現(xiàn)圖片上傳加預覽功能
這篇文章主要介紹了ThinkPHP5+Layui實現(xiàn)圖片上傳加預覽功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08