设为首页 加入收藏

TOP

Yii2中多表关联查询
2019-09-17 14:36:29 】 浏览:52
Tags:Yii2 关联 查询

 

 

准备条件:

1、首先准备两张表:

customer(用户表)(id, name)

order(订单表)(id, customer_id, price)

customer 表和 order 表之间是一对多的关系,通过 customer_id 字段关联。

2、建立相应的模型文件 customer.php 和 order.php 文件。

 

关联查询:

customer.php文件添加getOrder()方法:

<?php
 
namespace app\models;
 
use Yii;
 
/**
 * This is the model class for table "customer".
 *
 * @property int $id
 * @property string $name 用户姓名
 */
class Customer extends \yii\db\ActiveRecord
{
    /**
     * 获取订单信息
     */
    public function getOrder()
    {
        // 一个用户对应多个订单,一对多的关系使用hasMany()关联
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])->asArray();
    }
 
}

order.php文件添加getCustomer()方法:

<?php
 
namespace app\models;
 
use Yii;
 
/**
 * This is the model class for table "order".
 *
 * @property int $id
 * @property int $customer_id 用户id
 * @property string $price 价格
 */
class Order extends \yii\db\ActiveRecord
{
    /**
     * 获取用户信息
     */
    public function getCustomer()
    {
        // 一个订单对应一个用户,一对一的关系使用hasOne()关联
        return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->asArray();
    }
}

 

使用:

// 查询客户名为张三的订单信息
$customer = Customer::find()->where(['name' => '张三'])->one();
// 可以通过$customer->getOrder()方法调用customer.php模型中getOrder()方法
$orders = $customer->getOrder()->all();
// 也可以使用$customer->order属性调用,
// 当程序没有找到order属性时,PHP会调用__get()函数,程序会自动寻找getOrder()方法调用
// 这里不用加all(),程序会自动识别,如果使用的是hasMany则加all(),hasOne则加上one()
$orders = $customer->order;
var_dump($orders);exit;
 
// 查询订单id为1的客户信息
$order = Order::find()->where(['id' => 1])->one();
// 调用order.php模型中getCustomer()方法
$customer = $order->customer;
var_dump($customer);exit;

 

with 和 joinWith 的使用:

使用上面的关联查询时有个问题就是数据量大的时候性能问题。

$customers = Customer::find()->all();   // 相当于执行 select * from customer
foreach ($customers as $customer) {
    $orders = $customer->order;  // 相当于执行 select * from order where customer_id = id;
}

假如$customers中有100条数据,则要循环查询100次,整个程序需要执行SQL语句101次。

这时可以使用with():

// 相当于执行了两条SQL语句 select * from customer
// select * from order where customer_id in(...)
$customers = Customer::find()->with('order')->asArray()->all();
foreach ($customers as $customer) {
    $orders = $customer['order'];  // 取得order表的关联数据
}

joinWith()的用法和with()差不多,joinWith()可以指定连接类型,默认LEFT JOIN连接。

 

注意点:

1、在模型中定义hasMany,hasOne方法时,最好不要加上all(),one()。调用$customer->order时程序会自动根据使用的是hasMany还是hasOne加上相应的all(), one()。

2、使用with(), joinWith() 查询时,如果在模型中定义hasMany,hasOne方法时加上了all(),one(),程序会报错。

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇dedecmsV5.7 任意文件上传漏洞修复 下一篇网站域名授权

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目