L
L
lzy2020-07-29 11:58:36
Yii
lzy, 2020-07-29 11:58:36

Call to a member function offset() on array?

Hey! The problem with pagination, in general, everything works, the request too. But I just can’t deal with offset and this error, tell me please

$R = 6371;  // earth's mean radius, km
        $connection = Yii::$app->getDb();
        $command = $connection
            ->createCommand(
                "select id, title, keywords, country, city, latitude, longitude, 
    round((((acos(sin(($lat*pi()/180)) * sin((latitude*pi()/180))+cos(($lat*pi()/180)) * cos((latitude*pi()/180)) * cos((($long- longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344), 2) as D
    from posts
    having D < $post_distance
    order by id desc"
            );
        $pages = new Pagination(['totalCount' => count($command->queryAll()), 'pageSize' => 5, 'forcePageParam' => false, 'pageSizeParam' => false]);

        $posts = ($command->queryAll())->offset($pages->offset)->limit($pages->limit)->all();


Error on this line $posts = ($command->queryAll())->offset($pages->offset)->limit($pages->limit)->all();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vilinyh, 2020-07-29
@lzy

$command->queryAll() returns an array of records, not an object.
and the methods offset(), limit() and all() are methods of the Query/ActiveQuery/ActiveRecord classes.
You can rewrite the query to use offset/limit in the query body itself, or use the Query object (or its descendants):

$query = (new yii\db\Query())
        ->select(...)
        ->from(...)
        ->where(...)
        ->having(...)
        ->groupBy(...)
        ->orderBy(...);

    $pages = Pagination(['totalCount' => $query->count(), ...]);
    $posts = $query->offset(...)->limit(...)->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question