There are two types of associations between tables in Yii2, which are used to specify the association between two models.
One-to-many: hasMany
One-to-one: hasOne (recommended learning: yii tutorial)
Return results: The return results of these two methods are yii\db\ActiveQuery objects
The first parameter: the class name of the associated model.
The second parameter: is an array, where the key is the attribute in the associated model and the value is the attribute in the current model.
Associated use
Now we get all the order information of a customer
$customer = Customer::findOne(1); $orders = $customer->orders; // 通过在Customer中定义的关联方法(getOrders())来获取这个客户的所有的订单。
The above two lines of code will generate the following sql statement
SELECT * FROM customer WHERE id=1; SELECT * FROM order WHERE customer_id=1;
Association result caching
If the customer's order changes, we call it again
$orders = $customer->orders;
When you get the order again, you will find that there is no change. The reason is that the database will only be queried when $customer->orders is executed for the first time, and the results will be cached, and sql will not be executed during subsequent queries.
So what if I want to execute sql again? You can execute
unset($customer->orders); $customer->orders;
and then you can fetch data from the database.
Code Description:
// 执行sql语句: SELECT * FROM customer WHERE id=1 $customer = Customer::findOne(1); //执行sql:SELECT * FROM order WHERE customer_id=1 $orders1 = $customer->orders; //这个不会执行sql,直接使用上面的缓存结果 $orders2 = $customer->orders; //如果中间的用户订单有变化,我们就不能从缓存中获取,要unset掉 unset($customer->orders); $orders2 = $customer->orders;
The above is the detailed content of How to use yii2 hasone. For more information, please follow other related articles on the PHP Chinese website!

This tutorial demonstrates Yii Framework's timestamp management. It details using TimestampBehavior for automatic created_at and updated_at updates, offering customization options and comparing it to manual updates, database triggers, and custom be

The article discusses best practices for deploying Yii applications in cloud-native environments, focusing on scalability, reliability, and efficiency through containerization, orchestration, and security measures.

This article compares the PHP frameworks Yii and Laravel. Yii prioritizes speed and structure, while Laravel emphasizes developer experience and flexibility. Though both handle large-scale applications, Yii offers superior raw performance, while La

This article introduces Yii, a high-performance PHP framework ideal for large-scale web applications. It highlights Yii's speed, security, and robust architecture (MVC), emphasizing its advantages over other frameworks like Laravel, Symfony, and Cod

This article analyzes Yii framework's strengths and weaknesses. It highlights Yii's high performance, robust security, rapid development capabilities, and extensibility, but also notes a steeper learning curve and potential complexity for smaller pr

This article compares Yii and ThinkPHP (TP) frameworks. The choice depends on project scale and developer experience. Yii, robust and mature, suits large, complex projects needing high performance. TP, simpler and faster for development, is better f

This article details how to call and organize common functions in Yii applications. It advocates encapsulating functions within classes, ideally in a dedicated app/helpers directory, for improved reusability and maintainability. Different approache

The article discusses key considerations for using Yii in serverless architectures, focusing on statelessness, cold starts, function size, database interactions, security, and monitoring. It also covers optimization strategies and potential integrati


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

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.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

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

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