国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home PHP Framework YII How to conditionally query in yii framework

How to conditionally query in yii framework

Dec 07, 2019 pm 03:33 PM
yii frame

How to conditionally query in yii framework

Conditional query

$customers = Customer::find()->where($cond)->all();

$cond is what we call conditions. The way of writing conditions also depends on the query data. There are differences, so how to write query conditions using yii2?

[[Simple condition]]

// SQL: (type = 1) AND (status = 2).  
$cond = ['type' => 1, 'status' => 2]   
// SQL:(id IN (1, 2, 3)) AND (status = 2)  
$cond = ['id' => [1, 2, 3], 'status' => 2]   
//SQL:status IS NULL  
$cond = ['status' => null]

[and]: Combine different conditions together, usage example:

//SQL:`id=1 AND id=2`  
$cond = ['and', 'id=1', 'id=2']  
//SQL:`type=1 AND (id=1 OR id=2)`  
$cond = ['and', 'type=1', ['or', 'id=1', 'id=2']]  
//SQL:`type=1 AND (id=1 OR id=2)` //此寫(xiě)法'='可以換成其他操作符,例:in like != >=等  
$cond = [  
    'and',  
    ['=', 'type', 1],  
    [  
        'or',  
        ['=', 'id', '1'],  
        ['=', 'id', '2'],  
    ]  
]

[[or]]:

/SQL:`(type IN (7, 8, 9) OR (id IN (1, 2, 3)))`  
$cond = ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]

[[not]]:

//SQL:`NOT (attribute IS NULL)`  
$cond = ['not', ['attribute' => null]]

[[between]]: not between has the same usage

//SQL:`id BETWEEN 1 AND 10`  
$cond = ['between', 'id', 1, 10]

[[in]]: not in has the same usage

//SQL:`id IN (1, 2, 3)`  
$cond = ['in', 'id', [1, 2, 3]] or $cond = ['id'=>[1, 2, 3]]
//IN條件也適用于多字段  
$cond = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]  
//也適用于內(nèi)嵌sql語(yǔ)句  
$cond = ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]

[[like]]:

//SQL:`name LIKE '%tester%'`
$cond = ['like', 'name', 'tester']
//SQL:`name LIKE '%test%' AND name LIKE '%sample%'`
$cond = ['like', 'name', ['test', 'sample']]
//SQL:`name LIKE '%tester'`
$cond = ['like', 'name', '%tester', false]

[[exists]]: not exists usage is similar to

//SQL:EXISTS (SELECT "id" FROM "users" WHERE "active"=1)
$cond = ['exists', (new Query())->select('id')->from('users')->where(['active' => 1])]

In addition, you can specify any operator as follows:

//SQL:`id >= 10`
$cond = ['>=', 'id', 10] 
//SQL:`id != 10`
$cond = ['!=', 'id', 10]

PHP Chinese website, There are a large number of free Yii introductory tutorials, everyone is welcome to learn!

The above is the detailed content of How to conditionally query in yii framework. 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

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 evaluate the cost-effectiveness of commercial support for Java frameworks How to evaluate the cost-effectiveness of commercial support for Java frameworks Jun 05, 2024 pm 05:25 PM

Evaluating the cost/performance of commercial support for a Java framework involves the following steps: Determine the required level of assurance and service level agreement (SLA) guarantees. The experience and expertise of the research support team. Consider additional services such as upgrades, troubleshooting, and performance optimization. Weigh business support costs against risk mitigation and increased efficiency.

How does the learning curve of PHP frameworks compare to other language frameworks? How does the learning curve of PHP frameworks compare to other language frameworks? Jun 06, 2024 pm 12:41 PM

The learning curve of a PHP framework depends on language proficiency, framework complexity, documentation quality, and community support. The learning curve of PHP frameworks is higher when compared to Python frameworks and lower when compared to Ruby frameworks. Compared to Java frameworks, PHP frameworks have a moderate learning curve but a shorter time to get started.

How do the lightweight options of PHP frameworks affect application performance? How do the lightweight options of PHP frameworks affect application performance? Jun 06, 2024 am 10:53 AM

The lightweight PHP framework improves application performance through small size and low resource consumption. Its features include: small size, fast startup, low memory usage, improved response speed and throughput, and reduced resource consumption. Practical case: SlimFramework creates REST API, only 500KB, high responsiveness and high throughput

What is the difference between php framework laravel and yii What is the difference between php framework laravel and yii Apr 30, 2025 pm 02:24 PM

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

Yii with Docker: Containerizing and Deploying Your Applications Yii with Docker: Containerizing and Deploying Your Applications Apr 02, 2025 pm 02:13 PM

The steps to containerize and deploy Yii applications using Docker include: 1. Create a Dockerfile and define the image building process; 2. Use DockerCompose to launch Yii applications and MySQL database; 3. Optimize image size and performance. This involves not only specific technical operations, but also understanding the working principles and best practices of Dockerfile to ensure efficient and reliable deployment.

How to choose the best golang framework for different application scenarios How to choose the best golang framework for different application scenarios Jun 05, 2024 pm 04:05 PM

Choose the best Go framework based on application scenarios: consider application type, language features, performance requirements, and ecosystem. Common Go frameworks: Gin (Web application), Echo (Web service), Fiber (high throughput), gorm (ORM), fasthttp (speed). Practical case: building REST API (Fiber) and interacting with the database (gorm). Choose a framework: choose fasthttp for key performance, Gin/Echo for flexible web applications, and gorm for database interaction.

Java Framework Learning Roadmap: Best Practices in Different Domains Java Framework Learning Roadmap: Best Practices in Different Domains Jun 05, 2024 pm 08:53 PM

Java framework learning roadmap for different fields: Web development: SpringBoot and PlayFramework. Persistence layer: Hibernate and JPA. Server-side reactive programming: ReactorCore and SpringWebFlux. Real-time computing: ApacheStorm and ApacheSpark. Cloud Computing: AWS SDK for Java and Google Cloud Java.

What are the common misunderstandings in the learning process of Golang framework? What are the common misunderstandings in the learning process of Golang framework? Jun 05, 2024 pm 09:59 PM

There are five misunderstandings in Go framework learning: over-reliance on the framework and limited flexibility. If you don’t follow the framework conventions, the code will be difficult to maintain. Using outdated libraries can cause security and compatibility issues. Excessive use of packages obfuscates code structure. Ignoring error handling leads to unexpected behavior and crashes.

See all articles