


Yii2 framework implements analysis of common 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();
##Query:
Query:$rows = (new \yii\db\Query()) ->select(['code', 'name', 'population']) ->from('country') ->limit(10) ->all();
$data = $query->select(['code', 'name'])->from('country')->all(); // 调用 yii\db\Query::addSelect() 方法来选取附加字段 $data = $query->select(['code', 'name']) ->addSelect(['population'])->all();
$query->from('country'); $query->from(['public.country c']); $query->from('public.country c');
andFilterWhere() orFilterWhere()
Add
$country->name = 'UK'; $country->save();
Modify
$country = Customer::findOne($id); $country->email = 'UK'; $country->save(); // 等同于 $country->update();
Delete
$country = Country::findOne($id); $country->delete();
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(); //返回一个值指示是否包含查询结果的数据行;
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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

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

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

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