【Yii2】使用indexBy解决查询结果不重复的问题
问题描述
当我们使用ActiveRecord进行连表查询的时候,如果主表和关联表的关系是一对多的,就会在查询结果中出现多条主表id相同的数据
但是经过ActiveRecord查询返回的结果分页后,得到的数据就没有重复,对应的数据就会少
给我们直接的感觉是分页下来这个页面的数据不够一页的
分析原因
经过查看Yii2的源代码,我发现在all操作内,会做一次去重操作
代码位置 vendor/yiisoft/yii2/db/Query.php
/**
* Converts the raw query results into the format as specified by this query.
* This method is internally used to convert the data fetched from database
* into the format as required by this query.
* @param array $rows the raw query result from database
* @return array the converted query result
*/
public function populate