search
HomeBackend DevelopmentPHP TutorialYii2 framework implements analysis of common database operations
Yii2 framework implements analysis of common database operationsJun 15, 2018 pm 02:58 PM
yiiyii2Database 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();

Select:

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

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

From:

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

Where:

String format, for example: 'status=1'

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

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

andFilterWhere()

orFilterWhere()

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();

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();  //返回一个值指示是否包含查询结果的数据行;

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
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
php如何使用CodeIgniter4框架?php如何使用CodeIgniter4框架?May 31, 2023 pm 02:51 PM

PHP是一种非常流行的编程语言,而CodeIgniter4是一种常用的PHP框架。在开发Web应用程序时,使用框架是非常有帮助的,它可以加速开发过程、提高代码质量、降低维护成本。本文将介绍如何使用CodeIgniter4框架。安装CodeIgniter4框架CodeIgniter4框架可以从官方网站(https://codeigniter.com/)下载。下

如何使用宝塔面板进行MySQL管理如何使用宝塔面板进行MySQL管理Jun 21, 2023 am 09:44 AM

宝塔面板是一种功能强大的面板软件,它可以帮助我们快速部署、管理和监控服务器,尤其是经常需要进行网站搭建、数据库管理以及服务器维护的小型企业或个人用户。在这些任务中,MySQL数据库管理在很多情况下是一个重要的工作。那么如何使用宝塔面板进行MySQL管理呢?接下来,我们将逐步介绍。第一步:安装宝塔面板在开始使用宝塔面板进行MySQL管理之前,首先需要安装宝塔面

yii2 怎么去掉jqueryyii2 怎么去掉jqueryFeb 17, 2023 am 09:55 AM

yii2去掉jquery的方法:1、编辑AppAsset.php文件,注释掉变量$depends里的“yii\web\YiiAsset”值;2、编辑main.php文件,在字段“components”下面添加配置为“'yii\web\JqueryAsset' => ['js' => [],'sourcePath' => null,],”即可去掉jquery脚本。

使用PDO进行数据库操作:PHP的一个更好的方式使用PDO进行数据库操作:PHP的一个更好的方式Jun 21, 2023 pm 01:36 PM

使用PDO进行数据库操作:PHP的一个更好的方式在Web开发中,使用数据库进行数据存储、管理和查询是非常常见的。而PHP作为一种广泛应用于Web开发的语言,自然也提供了丰富的数据库操作方式。在PHP中,可以使用MySQLi、PDO以及其他扩展库来进行数据库操作。其中,PDO是一种非常常用的数据库操作方式,相比于其他方式有更多的优点。本文将介绍什么是PDO,以

如何使用thinkorm来提高数据库操作效率如何使用thinkorm来提高数据库操作效率Jul 28, 2023 pm 03:21 PM

如何使用thinkorm来提高数据库操作效率随着互联网的迅速发展,越来越多的应用程序需要进行大量的数据库操作。在这个过程中,数据库操作的效率问题就变得尤为重要。为了提高数据库操作效率,我们可以使用thinkorm这个强大的ORM框架来进行数据库操作。本文将介绍如何使用thinkorm来提高数据库操作效率,并通过代码示例来说明。一、什么是thinkormthi

如何在Symfony框架中使用Doctrine ORM进行数据库操作如何在Symfony框架中使用Doctrine ORM进行数据库操作Jul 29, 2023 pm 04:13 PM

如何在Symfony框架中使用DoctrineORM进行数据库操作引言:Symfony框架是一个流行的PHP框架,它提供了许多强大的工具和组件,用于快速而简单地构建Web应用程序。其中一个关键的组件是DoctrineORM,它提供了一种优雅的方式来处理数据库操作。本文将详细介绍在Symfony框架中如何使用DoctrineORM来进行数据库操作。我们将

精选几道CTF习题,带你学习yii2框架!精选几道CTF习题,带你学习yii2框架!Feb 23, 2022 am 10:33 AM

本篇文章带大家了解yii2框架,分享几道CTF习题,通过它们来学习yii2框架,希望对大家有所帮助。

如何使用Flask-SQLAlchemy进行数据库操作如何使用Flask-SQLAlchemy进行数据库操作Aug 02, 2023 am 08:39 AM

如何使用Flask-SQLAlchemy进行数据库操作Flask-SQLAlchemy是一种方便的扩展,可以在Flask应用中操作数据库。它提供了简单的API,以减少开发人员的工作量,并且与Flask框架无缝集成。本文将介绍如何使用Flask-SQLAlchemy进行数据库操作并提供代码示例。安装Flask-SQLAlchemy首先,需要安装Flask-SQ

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools