Different query return in ActiveRecord

I’m doing a simple query

$query = Model::find()->select(['field1', 'field2'])->all();

Why if I doing the query like this:

$query = Model::find();
$query->select(['field1', 'field2']);

Why result not is same, I mean, the first query return the sql result, the second return a ActiveQuery Object whithout query result, why happend this?

Hi @alejosv,

In the above, $query is not a query object, but a query result of ActiveRecord objects.
So you should have written it like this:

$models = Model::find()->select(['field1', 'field2'])->all();

In the above, $query is a query object, but you’ve forgot to populate a variable with the query result.

    $query = Model::find();
    $query->select(['field1', 'field2']);
    $models = $query->all();

Note that $query is still a query object after retrieving the result with all().

Hi @softark thanks, now is more clear for me