This article mainly introduces how to use the ActiveRecord mode in yii2, and analyzes the specific steps and related operation methods of using the ActiveRecord mode in yii2, for more information about how to use the Active Record mode in yii2. We will share this with you 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 (access link http: // localhost/basic/web/index. php? R = gii ):
Use ModelGenerator and CURD Generator to automatically generate the corresponding model code and add, delete, modify, and query code.
3. When the tables in the Database need to be modified, you can use migration:
Execute the command in the project:./yii migrate/create "custom name"
A new folder migrations is generated in the project to open the files 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:./yii migrate in the command line to execute the up function in the script.
Use the command:./yii migrate/down in the command line to execute the down Function in the script.
I hope this article will help you design PHP programs based on the Yii framework.