Answer the question
In order to leave comments, you need to log in
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();
Answer the question
In order to leave comments, you need to log in
$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 questionAsk a Question
731 491 924 answers to any question