YII dynamic model (Dynamic table name) supports analysis, yii Model
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;}}*/