YII動態模型(動態表名)支援分析,yii模型_PHP教程

來源:互聯網
上載者:User

YII動態模型(動態表名)支援分析,yii模型


本文分析了YII動態模型(動態表名)支援機制。分享給大家供大家參考,具體如下:

給YII 架構增加動態模型支援

Yii架構中的資料模型使用靜態機制,如果要使用模型方式操作某張資料表,就必須得事先建立資料表對應的模型類(位於 protected/models 目錄下),這種方式,在有的情況下給我們的工作帶來了一些不便,如僅僅將資料表進行顯示,或者資料表是動態產生的,或者要實現資料表模型中的讀寫分離,(如資料寫入與資料呈現邏輯可能定義到不同的模型中,以提高效能,如前背景分離)。

為解決這個問題,經過我反覆調試,已經為Yii 擴充出了動態資料表模型支援,使用時簡單提供表名,即可將其當作普通的資料表模型進行操作,當然帶來的問題就是無資料驗證。即使是這樣,也給資料顯示帶來極大的方便。如果在使用的過程中有任何問題,可隨時聯絡筆者信箱 zhangxugg@163.com 進行探討或索取源碼。

處理方法如下:

請將我提供的DbTable.php 放置到 protected/models/ 目錄下,然後就可以在任何位置使用之。

產生新記錄:

$memo = new DTable('{{memo}}');$memo->msg = 'this is content';$memo->save();//last insertidecho $memo->id ;

讀取已有記錄:

$memo = DTable::model('{{memo}}')->findByPk(12);$memo->msg = "modefid content";$memo->save();//使用非預設資料庫,需要在 config/main.php 檔案中定義資料庫連接,如: '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優秀開發架構總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於Yii架構的PHP程式設計有所協助。

您可能感興趣的文章:

  • PHP的Yii架構中過濾器相關的使用總結
  • PHP的Yii架構中View視圖的使用進階
  • 詳解PHP的Yii架構中的Controller控制器
  • Yii資料庫緩衝執行個體分析
  • Yii架構上傳圖片用法總結
  • Yii開啟片段快取的方法
  • 詳解PHP的Yii架構中組件行為的屬性注入和方法注入
  • 詳解在PHP的Yii架構中使用行為Behaviors的方法
  • 深入講解PHP的Yii架構中的屬性(Property)
  • 解讀PHP的Yii架構中請求與響應的處理流程
  • YII Framework的filter過濾器用法分析

http://www.bkjia.com/PHPjc/1117086.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1117086.htmlTechArticleYII動態模型(動態表名)支援分析,yii模型 本文分析了YII動態模型(動態表名)支援機制。分享給大家供大家參考,具體如下: 給YII 架構增加動...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.