我有这个应该返回一个数组的ActiveQuery:
$logData = VisitorLog::find()
->select($reportMetaHash[$selectedType]['select'])
->from("visitor_log v");
foreach($reportMetaHash[$selectedType]['joins'] as $join){
$logData->join($join['type'],$join['table'],$join['on']);
}
$logData->where(['=', 'v.app_id' , $app_id])
->andWhere(['=', 'assorted_id' , $selectedType])
->andWhere(['>=', 'access_time', $app->start_date])
->andWhere('assorted_id IN ('.$selectedType.')')
->groupBy("v.access_log_id, v.content_id")
->orderBy('b.booth_name, u.`first_name`')
->asArray()
->all();
echo '<pre>'; print_r($logData); exit;
此返回和ActiveQuery
对象而不是预期的数组。
但是当我添加没有循环的连接时,如下所示,我得到了一个数组,正如预期的那样。
$logData = VisitorLog::find()
->select($reportMetaHash[$selectedType]['select'])
->from("visitor_log v")
->join("JOIN", "user u", "u.`app_id`=".$app_id." AND u.id = v.access_log_id AND u.`user_type_id`=8")
->join("JOIN", "document d", "d.doc_id = v.`content_id` AND d.`doctypeId` = 1")
->join("JOIN", "booths b", "b.booth_id = d.booth_id")
->where(['=','v.app_id' , $app_id])
->andWhere(['=', 'assorted_id' , $selectedType])
->andWhere(['>=', 'access_time', $app->start_date])
->andWhere('assorted_id IN ('.$selectedType.')')
->groupBy("v.`access_log_id`, v.content_id")
->orderBy('b.booth_name, u.`first_name`')
->asArray()
->all();
链接应该在循环中工作,就像我认为没有它一样。我在这做错了什么?
PS:当我在SQL中运行相同的原始查询时,它返回正确的结果集。
答案 0 :(得分:2)
all()
调用返回查询结果 - 此方法不修改查询对象,只返回结果。您应该将其结果传递给变量:
$result = $logData->where(['=', 'v.app_id' , $app_id])
->andWhere(['=', 'assorted_id' , $selectedType])
->andWhere(['>=', 'access_time', $app->start_date])
->andWhere('assorted_id IN ('.$selectedType.')')
->groupBy("v.access_log_id, v.content_id")
->orderBy('b.booth_name, u.`first_name`')
->asArray()
->all();
echo '<pre>'; print_r($result); exit;