簡體   English   中英

在Yii2 ActiveRecord中獲取ActiveQuery而不是數組

[英]Getting ActiveQuery instead of array in Yii2 ActiveRecord

我有這個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中運行相同的原始查詢時,它將返回正確的結果集。

查詢結果由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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM