Home Backend Development PHP Tutorial Yii2 framework implements analysis of common database operations

Yii2 framework implements analysis of common database operations

Jun 15, 2018 pm 02:58 PM
yii yii2 Database operations

This article mainly introduces the summary of common operations of database implemented by Yii2 framework. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

General:

use yii\db\Query;
$query = new Query();
Copy after login

##Query:

Query:

$rows = (new \yii\db\Query())
      ->select(['code', 'name', 'population'])
      ->from('country')
      ->limit(10)
      ->all();
Copy after login

Select:

$data = $query->select(['code', 'name'])->from('country')->all();

// 调用 yii\db\Query::addSelect() 方法来选取附加字段
    $data = $query->select(['code', 'name'])
      ->addSelect(['population'])->all();
Copy after login

From:

$query->from('country'); 
$query->from(['public.country c']); 
$query->from('public.country c');
Copy after login

Where:

String format, for example: 'status=1'

Hash format, for example: ['status' => 1, 'type' => 2]

Operator format, for example: ['like', 'name', ' test']

andFilterWhere()

orFilterWhere()
Copy after login

Active Record (Active Record, hereinafter referred to as AR) provides an object-oriented interface to access data in the database. An AR class is associated with a data table, each AR object corresponds to a row in the table, and the attributes of the object (that is, the attribute of AR) are mapped to the corresponding columns of the data row. An activity record (AR object) corresponds to a row of the data table, and the attributes of the AR object map to the corresponding columns of the row.

All additions, deletions and modifications here will use AR objects for mapping operations.

Add

$country->name = 'UK';
$country->save();
Copy after login

Modify

$country = Customer::findOne($id);
$country->email = 'UK';
$country->save(); // 等同于 $country->update();
Copy after login

Delete

$country = Country::findOne($id);
$country->delete();
Copy after login

Others

User::find()->all();  //返回所有用户数据;
User::findOne($id);  //返回 主键 id=1 的一条数据; 
User::find()->where(['name' => 'ttt'])->one();  //返回 ['name' => 'ttt'] 的一条数据;
User::find()->where(['name' => 'ttt'])->all();  //返回 ['name' => 'ttt'] 的所有数据;
User::findBySql('SELECT * FROM user')->all(); //用 sql 语句查询 user 表里面的所有数据;
User::findBySql('SELECT * FROM user')->one(); //此方法是用 sql 语句查询 user 表里面的一条数据;
User::find()->andWhere(['sex' => '女', 'age' => '18'])->count('id');  //统计符合条件的总条数;
User::find()->one();  //返回一条数据;
User::find()->all();  //返回所有数据;
User::find()->count();  //返回记录的数量;
User::find()->average();  //返回指定列的平均值;
User::find()->min();  //返回指定列的最小值 ;
User::find()->max();  //返回指定列的最大值 ;
User::find()->scalar();  //返回值的第一行第一列的查询结果;
User::find()->column();  //返回查询结果中的第一列的值;
User::find()->exists();  //返回一个值指示是否包含查询结果的数据行;
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study, For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

yii2 component implements drop-down box search function

How Yii2 implements multiple searches at the same time Fields

About Yii Model query based on arrays and objects

##

The above is the detailed content of Yii2 framework implements analysis of common database operations. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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 use PHP scripts to perform database operations in Linux environment How to use PHP scripts to perform database operations in Linux environment Oct 05, 2023 pm 03:48 PM

How to use PHP to perform database operations in a Linux environment. In modern web applications, the database is an essential component. PHP is a popular server-side scripting language that can interact with various databases. This article will introduce how to use PHP scripts for database operations in a Linux environment and provide some specific code examples. Step 1: Install the Necessary Software and Dependencies Before starting, we need to ensure that PHP and related dependencies are installed in the Linux environment. usually

How to use Pagoda Panel for MySQL management How to use Pagoda Panel for MySQL management Jun 21, 2023 am 09:44 AM

Pagoda Panel is a powerful panel software that can help us quickly deploy, manage and monitor servers, especially small businesses or individual users who often need to build websites, database management and server maintenance. Among these tasks, MySQL database management is an important job in many cases. So how to use the Pagoda panel for MySQL management? Next, we will introduce it step by step. Step 1: Install Pagoda Panel. Before starting to use Pagoda Panel for MySQL management, you first need to install Pagoda Panel.

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

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

Using PDO for Database Operations: A Better Way with PHP Using PDO for Database Operations: A Better Way with PHP Jun 21, 2023 pm 01:36 PM

Using PDO for database operations: A better way with PHP In web development, it is very common to use databases for data storage, management, and query. As a language widely used in Web development, PHP naturally provides a wealth of database operation methods. In PHP, you can use MySQLi, PDO and other extension libraries to perform database operations. Among them, PDO is a very commonly used database operation method and has more advantages than other methods. This article will introduce what PDO is to

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

How to use thinkorm to improve database operation efficiency How to use thinkorm to improve database operation efficiency Jul 28, 2023 pm 03:21 PM

How to use thinkorm to improve database operation efficiency With the rapid development of the Internet, more and more applications require a large number of database operations. In this process, the efficiency of database operations becomes particularly important. In order to improve the efficiency of database operations, we can use thinkorm, a powerful ORM framework, to perform database operations. This article will introduce how to use thinkorm to improve the efficiency of database operations and illustrate it through code examples. 1. What is thinkormthi?

See all articles