from(Shop::tableName()); $query->where(['ptStyle' => 1]); $query->orderBy('addTime ASC'); foreach ($query->batch(20) as $shopList) { foreach ($shopList as $shop) { $shopId = $shop['id']??0; $query = new \yii\db\Query(); $query->from(Custom::tableName()); $query->where(['shopId' => $shopId]); $query->orderBy('addTime ASC'); foreach ($query->batch(50) as $customList) { foreach ($customList as $custom) { } } } } } }