Home >Backend Development >PHP Tutorial >How to use Active Record mode in yii2, yii2record_PHP tutorial
The example in this article describes the method of using Active Record mode in yii2. Share it with everyone for your reference, the details are as follows:
1. Configure the corresponding database information in db.php:
return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2basic', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ];
2. Use the gii module to automatically generate the corresponding code (visit the link http://localhost/basic/web/index.php?r=gii):
Use ModelGenerator and CURD Generator to automatically generate corresponding model code and code for addition, deletion, modification and query
3. When the table in the database needs to be modified, you can use migration:
Execute the command in the project: ./yii migrate/create “define your own name”
A new folder migrations will be generated in the project, open the file in the folder:
class m150225_022640_modify_book_table extends Migration { public function up() { $this->addColumn("book", "book_desc", yii\db\mssql\Schema::TYPE_TEXT); } public function down() { echo "m150225_022640_modify_book_table cannot be reverted.\n"; return false; } }
Use the command in the command line: ./yii migrate to execute the up function in the script
Use the command in the command line: ./yii migrate/down to execute the down function in the script
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.