search
HomePHP FrameworkThinkPHPHow to use array query object in thinkphp5.1

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;

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,
];

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

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

, the generated SQL is:

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

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

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

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

. The generated SQL is:

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

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
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment