您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

Yii2中join、joinwith多表关联查询的使用方法

2024/4/29 0:33:23发布5次查看
本文主要介绍了yii2中多表关联查询(join、joinwith)的使用方法,需要的朋友可以参考下。希望对大家有所帮助。
表结构
现在有客户表、订单表、图书表、作者表,
客户表customer (id customer_name)
订单表order (id order_name customer_id book_id)
图书表 (id book_name author_id)
作者表 (id author_name)
模型定义
下面是这4个个模型的定义,只写出其中的关联
customer
class customer extends \yii\db\activerecord { // 这是获取客户的订单,由上面我们知道这个是一对多的关联,一个客户有多个订单 public function getorders() { // 第一个参数为要关联的子表模型类名, // 第二个参数指定 通过子表的customer_id,关联主表的id字段 return $this->hasmany(order::classname(), ['customer_id' => 'id']); } }
order
class order extends \yii\db\activerecord { // 获取订单所属用户 public function getcustomer() { //同样第一个参数指定关联的子表模型类名 // return $this->hasone(customer::classname(), ['id' => 'customer_id']); } // 获取订单中所有图书 public function getbooks() { //同样第一个参数指定关联的子表模型类名 // return $this->hasmany(book::classname(), ['id' => 'book_id']); } }
book
class book extends \yii\db\activerecord { // 获取图书的作者 public function getauthor() { //同样第一个参数指定关联的子表模型类名 return $this->hasone(author::classname(), ['id' => 'author_id']); } }
author
class autor extends \yii\db\activerecord { }
hasmany、hasone使用
yii2中的表之间的关联有2种,它们用来指定两个模型之间的关联。
一对多:hasmany
一对一:hasone
返回结果:这两个方法的返回结果都为yii\db\activequery对象
第一个参数:所关联的模型的类名称。
第二个参数:是一个数组,其中键为所关联的模型中的属性,值为当前模型中的属性。
关联的使用
现在我们获取一个客户的所有的订单信息
// 获取一个客户信息 $customer = customer::findone(1); $orders = $customer->orders; // 通过在customer中定义的关联方法(getorders())来获取这个客户的所有的订单。
上面的两行代码会生成如下sql语句
select * from customer where id=1; select * from order where customer_id=1;
关联结果缓存
如果客户的订单改变了,我们再重新调用
$orders = $customer->orders;
再次得到订单的时候你会发现没有变化。原因是只会在第一次执行$customer->orders的时候才会去数据库里面查询,然后会把结果缓存起来,以后查询的时候都不会再执行sql。
那么如果我想再次执行sql如何做呢?可以执行
unset($customer->orders); $customer->orders;
然后就可以从数据库里面取数据了。
定义多个关联
同样,我们还可以在customer里面定义多个关联。
如返回总数大于100的订单。
class customer extends \yii\db\activerecord { public function getbigorders($threshold = 100) { return $this->hasmany(order::classname(), ['customer_id' => 'id']) ->where('subtotal > :threshold', [':threshold' => $threshold]) ->orderby('id'); } }
关联的两种访问方式
如上面的,如果使用
$customer->bigorders
将会得到大于100的所有的订单。如果要返回大于200的订单可以这样写
$orders = $customer->getbigorders(200)->all();
从上面可以看出访问一个关联的时候有两种方法
如果以函数的方式调用,会返回一个 activequery 对象($customer->getorders()->all())
如果以属性的方式调用,会直接返回模型的结果($customer->orders)
with的使用看如下代码,是取一个客户的订单
// 执行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;
如果现在我们要取出100个用户,然后访问每个用户的订单,从上面的了解我们可能会写出如下代码
// 执行sql语句: select * from customer limit 100 $customers = customer::find()->limit(100)->all(); foreach ($customers as $customer) { // 执行sql: select * from order where customer_id=... $orders = $customer->orders; // 处理订单。。。 }
然而,如果真要这样写的话,会在foreach的每个循环里面都执行一次sql去数据库里面查询数据。因为每个$customer对象都是不一样的。
为了解决上面的问题 可以使用 yii\db\activequery::with()。
其中width的参数为关系的名称,也就在model里面定义的getorders,getcustomer中的orders和customer
// 先执行sql: select * from customer limit 100; // select * from orders where customer_id in (1,2,...) $customers = customer::find()->limit(100) ->with('orders')->all(); foreach ($customers as $customer) { // 在这个循环的时候就不会再执行sql了 $orders = $customer->orders; // ...handle $orders... }
如果使用了select来指定返回的列,一定要确保返回的列里面包含所关联的模型的关联字段,否则将不会返回关联的表的model
$orders = order::find()->select(['id', 'amount'])->with('customer')->all(); // $orders[0]->customer 的结果将会是null // 因为上面的select中没有返回所关联的模型(customer)中的指定的关联字段。 // 如果加上customer_id,$orders[0]->customer就可以返回正确的结果 $orders = order::find()->select(['id', 'amount', 'customer_id'])->with('customer')->all();
给with加过滤条件
查询一个客户大于100的订单
//首先执行sql: select * from customer where id=1 $customer = customer::findone(1); // 再执行查询订单的sql语句:select * from order where customer_id=1 and subtotal>100 $orders = $customer->getorders()->where('subtotal>100')->all();
查询100个客户的,每个客户的总合大于100的订单
// 下面的代码会执行sql语句: // select * from customer limit 100 // select * from order where customer_id in (1,2,...) and subtotal>100 $customers = customer::find()->limit(100)->with([ 'orders' => function($query) { $query->andwhere('subtotal>100'); }, ])->all();
在这里width的参数为数组,键为关联的名称,值为回调函数。
也就是说 对orders这个关联返回的activequery,再执行一次$query->andwhere('subtotal>100');
使用joinwith进行表关联
我们都知道可以用join on来写多个表之间的关联。先看看yii2中joinwit的声明
joinwith( $with, $eagerloading = true, $jointype = 'left join' )
$with 数据类型为字符串或数组,如果为字符串,则为模型中定义的关联的名称(可以为子关联)。
如果为数组,键为model中以getxxx格式定义的关联,值为对这个关联的进一步的回调操作。
$eagerloading 是否加载在$with中关联的模型的数据。
$jointype 联接类型,可用值为:left join、inner join,默认值为left join
// 订单表和客户表以left join的方式关联。 // 查找所有订单,并以客户 id 和订单 id 排序 $orders = order::find()->joinwith('customer')->orderby('customer.id, order.id')->all(); // 订单表和客户表以inner join的方式关联 // 查找所有的订单和书 $orders = order::find()->innerjoinwith('books')->all(); // 使用inner join 连接order中的 books关联和customer关联。 // 并对custmer关联再次进行回调过滤:找出24小时内注册客户包含书籍的订单 $orders = order::find()->innerjoinwith([ 'books', 'customer' => function ($query) { $query->where('customer.created_at > ' . (time() - 24 * 3600)); } ])->all(); // 使用left join连接 books关联,books关联再用left join 连接 author关联 $orders = order::find()->joinwith('books.author')->all();
在实现上,yii 先执行满足join查询条件的sql语句,把结果填充到主模型中, 然后再为每个关联执行一条查询语句, 并填充相应的关联模型。
// order和books关联 inner join ,但不获取books关联对应的数据 $orders = order::find()->innerjoinwith('books', false)->all();
on条件
在定义关联的时候还可以指定on条件
class user extends activerecord { public function getbooks() { return $this->hasmany(item::classname(), ['owner_id' => 'id'])->oncondition(['category_id' => 1]); } }
在joinwith中使用
//先查询主模型(user)的数据, select user.* from user left join item on item.owner_id=user.id and category_id=1 // 然后再根据关联条件查询相关模型数据select * from item where owner_id in (...) and category_id=1 // 这两个在查询的过程中都使用了 on条件。 $users = user::find()->joinwith('books')->all();
如果没有使用join操作,即使用的是with 或者 直接以属性来访问关联。这个时候on条件会作为where 条件。
// select * from user where id=10 $user = user::findone(10);
总结
首先需要在模型中定义好关联(如getorders中的orders为一个关联)
然后在with或者joinwith中使用在模型中定义的关联。
其中在使用关联的时候还可以指定回调方法。
再有就是可以给关联、with、joinwith指定where或者on条件。
相关推荐:
yii2实现qq互联登录
yii2使用缓存的简单解析
yii2实现rbac权限控制
以上就是yii2中join、joinwith多表关联查询的使用方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录