This paper analyzes the support mechanism of YII dynamic model (dynamic table name). Share to everyone for your reference, specific as follows:
Add dynamic model support to YII framework
The data model in the YII framework uses a static mechanism, and if you want to manipulate a data table using a model, you have to create a data table corresponding to the model class (located in the Protected/models directory), which in some cases brings some inconvenience to our work, If the data table is only displayed, or the data table is dynamically generated, or to implement the read-write separation in the datasheet model, such as data writing and data rendering logic may be defined in different models to improve performance, such as the separation of the former background.
To solve this problem, after my repeated debugging, has been extended for YII Dynamic Data table model support, the use of a simple table name can be used as a normal data table model to operate, of course, the problem is no data validation. Even so, it brings great convenience to the data display. If there are any problems in the use of the process, you can always contact the author's mailbox zhangxugg@163.com to explore or request source code.
The processing method is as follows:
Please place the dbtable.php I provided in the protected/models/directory and then use it anywhere.
To produce a new record:
$memo = new Dtable (' {{memo}} ');
$memo->msg = ' This is content ';
$memo->save ();
Last Insertid
echo $memo->id;
To read an existing record:
$memo = Dtable::model (' {{memo}} ')->findbypk ();
$memo->msg = "Modefid content";
$memo->save ();
Using a Non-default database, you need to define a database connection in the config/main.php file, such as:
' components ' => array ('
db-other ' =>array (
' class ' = > ' cdbconnection ',
' connectionString ' => ' Mysql:host=localhost;dbname=cdcol;charset=utf8 ',
' Username ' => ' root ', ' password ' => ', ' tableprefix ' => ', ' AutoConnect ' =>
false, '
c13/>);
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 ' =& Gt Array (' class ' => ' cdbconnection ', ' connectionString ' => ' mysql:host=localhost;dbname=cdcol;charset=utf8 ', ' username ' => ' root ', ' password ' => ', ' tableprefix ' => ', ' Autoconnec
T ' => 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 ();
The public static function model ($table _name= ') {self:: $tableName = $table _name;
Return Parent::model (__class__);
Public Function TableName () {return self:: $tableName;}}
*/
More about Yii related content readers can view the site topics: "Yii framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design based on the YII framework.