Home PHP Framework ThinkPHP How to use array query object in thinkphp5.1

How to use array query object in thinkphp5.1

Jul 08, 2021 am 09:19 AM
php thinkphp5

Developers who have used 5.0 are more dependent on the array query method of 5.0, but they are helpless about the difference between the array query method of 5.1 and 5.0 It is very large, so I often hear developers complain that array queries in 5.1 are not easy to use.

First of all, for reasons of security and ease of use, it is officially not recommended to use array query conditions. Secondly, you may not know that version 5.1 actually provides a new array object. The query method is used to replace the previous array condition.

If your version is V5.1.21, you can try the array object query method mentioned below, you will definitely have unexpected surprises^_^

For those who are accustomed to or rely heavily on array queries For users with conditions, you can choose array object query. This object completes the bridge between ordinary array query and system query expression. However, compared with the query expression method recommended by the system, you need to pay attention to the variables. Security to avoid SQL injection.

To use array object query, you first need to introduce the thinkdbWhere class.

use think\db\Where;
Copy after login

There are generally two ways to use Where objects. The first one is the simplest. You still use array conditions to define query conditions like 5.0, for example:

$map = [
    'name'   => ['like', 'thinkphp%'],
    'title'  => ['like', '%think%'],
    'id'     => ['>', 10],
    'status' => 1,
];
Copy after login

Then, in the actual use of where When changing the method to

Db::name('user')
    ->where(new Where($map))
    ->select();
Copy after login

, the generated SQL is:

SELECT * FROM `think_user` WHERE  `name` LIKE 'thinkphp%' AND `title` LIKE '%think%' AND  `id` > 10  AND `status` =1
Copy after login

This method is the easiest to modify, and is equivalent to switching to the 5.0 array query method with one click. Of course, in addition to Db queries, model queries are also supported.

The second way is to directly instantiate a Where object, and then directly pass in the Where object instance when querying the where method.

$where          = new Where;
$where['id']    = ['in', [1, 2, 3]];
$where['title'] = ['like', '%php%'];

Db::name('user')
    ->where($where)
    ->select();
Copy after login

Where object implements the ArrayAccess interface, so it can be assigned directly as an array.

The generated SQL is:

SELECT * FROM `think_user` WHERE   `id` IN (1,2,3) AND `title` LIKE '%php%'
Copy after login

Using Where object query can be mixed with other query methods. If you want to add parentheses to the query conditions of an array query object when you mix array query objects, you can use

$where          = new Where;
$where['id']    = ['in', [1, 2, 3]];
$where['title'] = ['like', '%php%'];

Db::name('user')
    ->where($where->enclose())
    ->where('status', 1)
    ->select();
Copy after login

. The generated SQL is:

SELECT * FROM `think_user` WHERE  ( `id` IN (1,2,3) AND `title` LIKE '%php%' ) AND  `status` =1
Copy after login

enclose method represents the query. The condition will be enclosed in parentheses on both sides.

When using array objects to query, please be sure to check the data type and try to avoid letting users determine your data, which may lead to the possibility of SQL injection.
Related recommendations: The latest 10 thinkphp video tutorials

The above is the detailed content of How to use array query object in thinkphp5.1. 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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles