J
J
Justique2018-02-14 10:35:52
Yii
Justique, 2018-02-14 10:35:52

Dynamic loading with saving the last id?

Good afternoon, at the moment dynamic loading by offset and limit is implemented

$offset = 0;
    $limit = 8;
    if (isset(Yii::$app->request->post()['offset'])) {
      $offset = (int)Yii::$app->request->post()['offset'];
    }

    $query = Product::findNewest($audience_rec ? $audience_rec->id : null);
    $query->limit($limit + 1);
    $query->offset($offset);

The problem is that if at the time of loading the goods, new records appear on the site, then there is an offset offset and the records are duplicated.
Somewhere I previously encountered such a problem and the solution was to pass the last id of the entry in the resulting list and, based on it, already form a request, so I don’t remember how such a design is implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Bay, 2018-02-14
@Justique

It is far from a fact that everything in your store will be sorted by ID. and you need to solve it somehow.
In the simplest version, for example, I currently have it as a stub like this:
$query = Model::find()
->andWhere('id<:last_id',['last_id'=>$last_id])
->orderBy( 'id desc')
->limit(20)
;
In the same view, we render the block and if there are 20 records, then we insert a button to load other records.
Note that sorting in this case should be by id.

E
Evgeny Bukharev, 2018-02-14
@evgenybuckharev

There is a more elegant solution for dynamic loading, but for the case if you use a DataProvider

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question