Method _ php instance using ActiveRecord mode in yii2

Source: Internet
Author: User
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.

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.