Table of Contents
How to use Active Record mode in yii2, yii2record
Articles you may be interested in:
Home Backend Development PHP Tutorial How to use Active Record mode in yii2, yii2record_PHP tutorial

How to use Active Record mode in yii2, yii2record_PHP tutorial

Jul 12, 2016 am 09:01 AM
active record yii yii2

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove jquery in yii2 How to remove jquery in yii2 Feb 17, 2023 am 09:55 AM

How to remove jquery from yii2: 1. Edit the AppAsset.php file and comment out the "yii\web\YiiAsset" value in the variable $depends; 2. Edit the main.php file and add the configuration "'yii" under the field "components" \web\JqueryAsset' => ['js' => [],'sourcePath' => null,]," to remove the jquery script.

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

Black Myth: Wukong smashes the competition with 2.2 million Steam players mere hours after launch Black Myth: Wukong smashes the competition with 2.2 million Steam players mere hours after launch Aug 21, 2024 am 10:25 AM

The hype for Black Myth: Wukong has been felt globally as the game slowly crawled towards its launch date, and it didn't disappoint when it launched on August 20, having received a very warm welcome from the gaming community at large. After being onl

See all articles