How to use Active Record mode in yii2, yii2record_PHP tutorial

WBOY
Release: 2016-07-12 09:01:07
Original
719 people have browsed it

How to use Active Record mode in yii2, yii2record

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',
];

Copy after login

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;
  }
}

Copy after login

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.

Articles you may be interested in:

  • Yii2 uses Bootbox plug-in to implement custom pop-up windows
  • Learn YII2 framework from scratch (4) Extension plug-in yii2-kartikgii
  • Learn the YII2 framework from scratch (3) Extend the plug-in yii2-gird
  • Learn the YII2 framework from scratch (2) Install the extension plug-in through Composer
  • Use the drop-down menu in the yii2 framework Automatic search yii-widget-select2 instance analysis
  • YII2.0 Activeform form component usage example
  • Implementation method of adding verification code in yii2
  • Yii2 framework reference bootstrap Methods of date plug-in yii2-date-picker

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1089936.htmlTechArticleHow to use Active Record mode in yii2, yii2record 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. In db.ph...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!