《PHP編程:YII動(dòng)態(tài)模型(動(dòng)態(tài)表名)支持分析》要點(diǎn):
本文介紹了PHP編程:YII動(dòng)態(tài)模型(動(dòng)態(tài)表名)支持分析,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
相關(guān)主題:YII框架
本文分析了YII動(dòng)態(tài)模型(動(dòng)態(tài)表名)支持機(jī)制.分享給大家供大家參考,具體如下:PHP實(shí)戰(zhàn)
給YII 框架增加動(dòng)態(tài)模型支持PHP實(shí)戰(zhàn)
Yii框架中的數(shù)據(jù)模型使用靜態(tài)機(jī)制,如果要使用模型方式操作某張數(shù)據(jù)表,就必須得事先創(chuàng)建數(shù)據(jù)表對(duì)應(yīng)的模型類(位于 protected/models 目錄下),這種方式,在有的情況下給我們的工作帶來(lái)了一些不便,如僅僅將數(shù)據(jù)表進(jìn)行顯示,或者數(shù)據(jù)表是動(dòng)態(tài)生成的,或者要實(shí)現(xiàn)數(shù)據(jù)表模型中的讀寫(xiě)分離,(如數(shù)據(jù)寫(xiě)入與數(shù)據(jù)呈現(xiàn)邏輯可能定義到不同的模型中,以提高性能,如前后臺(tái)的分離).PHP實(shí)戰(zhàn)
為辦理這個(gè)問(wèn)題,經(jīng)過(guò)我反復(fù)調(diào)試,已經(jīng)為Yii 擴(kuò)展出了動(dòng)態(tài)數(shù)據(jù)表模型支持,使用時(shí)簡(jiǎn)單提供表名,即可將其當(dāng)作普通的數(shù)據(jù)表模型進(jìn)行操作,當(dāng)然帶來(lái)的問(wèn)題就是無(wú)數(shù)據(jù)驗(yàn)證.即使是這樣,也給數(shù)據(jù)顯示帶來(lái)極大的方便.如果在使用的過(guò)程中有任何問(wèn)題,可隨時(shí)聯(lián)系筆者信箱 zhangxugg@163.com 進(jìn)行探討或索取源碼.PHP實(shí)戰(zhàn)
處理辦法如下:PHP實(shí)戰(zhàn)
請(qǐng)將我提供的DbTable.php 放置到 protected/models/ 目錄下,然后就可以在任何位置使用之.PHP實(shí)戰(zhàn)
產(chǎn)生新記錄:PHP實(shí)戰(zhàn)
$memo = new DTable('{{memo}}'); $memo->msg = 'this is content'; $memo->save(); //last insertid echo $memo->id ;
讀取已有記錄:PHP實(shí)戰(zhàn)
$memo = DTable::model('{{memo}}')->findByPk(12); $memo->msg = "modefid content"; $memo->save(); //使用非默認(rèn)數(shù)據(jù)庫(kù),需要在 config/main.php 文件中定義數(shù)據(jù)庫(kù)連接,如: '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.10PHP實(shí)戰(zhàn)
/** * 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; } } */
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門(mén)及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》PHP實(shí)戰(zhàn)
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所贊助.PHP實(shí)戰(zhàn)
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP編程:YII動(dòng)態(tài)模型(動(dòng)態(tài)表名)支持分析》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/7220.html