Olga
Olga

Reputation: 443

ActiveQuery to array (asArray)

What's wrong with that query?

$numery = Number::find()
        ->select('number.phone_number')
        ->from('number')
        ->leftJoin('ordered_number', ['ordered_number.number_id' => 'number.phone_number'])
        ->where(['ordered_number.order_id' => 123])
        ->asArray();

I'm not sure if asArray is used in good way and also if I have to end my query with one() or all() because all the examples I've seen have it.

I changed it to:

        $numery = Number::find()
                ->select('number.phone_number')
                ->from('number, ordered_number')
                ->where(['ordered_number.number_id' => 'number.id'])
                ->andWhere(['ordered_number.order_id' => 123])
                ->asArray()
                ->all();

But still I get NULL instead of 4-element array. When I delete the "where" part I get all the numbers I have, so I suppose there is a problem somewhere there. The query that works in raw MySQL and that I want to use looks like this:

SELECT number.phone_number 
FROM number, ordered_number 
WHERE ordered_number.number_id=number.id 
AND ordered_number.order_id=123

Upvotes: 3

Views: 1592

Answers (2)

Bizley
Bizley

Reputation: 18021

About the first one:

$numery = Number::find()
    ->select('number.phone_number')
    ->from('number')
    ->leftJoin('ordered_number', ['ordered_number.number_id' => 'number.phone_number'])
    ->where(['ordered_number.order_id' => 123])
    ->asArray();

You need to end it with one() or all() depending on the number of results you are expecting to get.
one() gives you single DB row, all() gives you array of DB rows.
Without asArray() it gives you object (or array of objects in case of all()) - with asArray() it gives you array (or array of arrays) obviously.

One more thing - when you use Model::find() static method you don't have to set from() as it will automatically use the underlying DB table for the model.

Update: leftJoin is wrong - it should be

->leftJoin('ordered_number', 'ordered_number.number_id = number.phone_number')

About second one:

$numery = Number::find()
            ->select('number.phone_number')
            ->from('number, ordered_number')
            ->where(['ordered_number.number_id' => 'number.id'])
            ->andWhere(['ordered_number.order_id' => 123])
            ->asArray()
            ->all();

You are adding two tables in from(). This method states that in that case yu should use array instead of string:

->from(['number', 'ordered_number'])

Upvotes: 1

marche
marche

Reputation: 1756

In your code $numery ends up holding the ActiveQuery object created by the find() method and modified by the other methods after it.

You need to use the one() or all() methods at the end so you execute the query and get back the result of the query.

The asArray() method just tells the ActiveQuery object to return it's result as an Array instead of an Object or Array of Objects.

Upvotes: 1

Related Questions