YII dynamic model (dynamic table name) supports analysis, yii model _ PHP Tutorial

Source: Internet
Author: User
The YII dynamic model (dynamic table name) supports analysis and the yii model. YII dynamic model (dynamic table name) supports analysis. This article analyzes the yii dynamic model (dynamic table name) support mechanism. For your reference, see: add dynamic YII model (dynamic table name) to the YII Framework for Analysis.

This article analyzes the YII dynamic model (dynamic table name) support mechanism. We will share this with you for your reference. The details are as follows:

Add dynamic model support for the YII Framework

The data model in the Yii Framework uses a static mechanism. to operate a data table using a model, you must first create a model class (under the protected/models directory) for the data table. in this way, in some cases, it may cause some inconvenience to our work, for example, simply displaying the data table, dynamically generating the data table, or implementing read/write splitting in the data table model, (for example, the data writing and data presentation logic may be defined in different models to improve performance, such as the separation of the front and back ends ).

To solve this problem, I have made repeated debugging and expanded the dynamic data table model support for Yii. a simple table name can be used as a common data table model, of course, there is no data verification. Even so, it brings great convenience to data display. If you have any problems in the process of use, you can contact the author mailbox zhangxugg@163.com to discuss or obtain the source code.

The solution is as follows:

Place the DbTable. php I provide in the protected/models/directory, and then you can use it anywhere.

New records generated:

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

Read existing records:

$ Memo = DTable: model ('{memo}')-> findByPk (12); $ memo-> msg = "modefid content "; $ memo-> save (); // use a non-default database, which must be in config/main. define database connections in the php file, for example, '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;}}*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.