0

I have this ActiveQuery that should return an array:

$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;

This return and ActiveQuery object instead of an expected array.

But when i add the joins without the loop, like below, i get an array, as expected.

$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();

The chaining should work in the loop just as it does without it i think. What am i doing wrong here?

PS: When i run the the same raw query in SQL it returns correct resultset.

1 Answer 1

2

Query result is returned by all() call - this method does not modify query object, it just returns the results. You should pass its result to variable:

$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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.