R
R
Roman Savitsky2017-02-20 13:15:05
Yii
Roman Savitsky, 2017-02-20 13:15:05

How to display pagination correctly now?

Good afternoon everyone.
Implemented the ability to add categories from the admin panel, and select a category when publishing a post.
Up to this point, the entries were displayed like this:
In Controllers:

public function actionPagestatji(){
        $query = Statjiblog::find();
        $pagination = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => $query->count()
        ]);

        $Bgstatji = $query->orderBy(['id' => SORT_DESC])
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        return $this->render('pagestatji', [
            'Bgstatji' => $query,
            'active_page' => Yii::$app->request->get("page", 1),
            'count_pages' => $pagination->getPageCount(),
            'pagination' => $pagination
        ]); 
    }

I connected it in the page template AND actually the pagination worked. At the moment I am displaying Posts immediately with Categories on the page In Models I wrote:use yii\widgets\LinkPager;
public function getCategorys()
    {
        return $this->hasOne(Category::className(),['id' => 'parent_category_id']);
    }

In Controllers:
public function actionPagestatji(){

        $query = Statjiblog::find()->with('categorys')->each();

        return $this->render('pagestatji', [
            'Bgstatji' => $query
        ]); 
    }

The pagination example above does not work in this case, it complains about 'totalCount' => $query->count()
How to implement pagination in this case?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-02-20
@PRC

Why did you change the action code? Everything was correct. Just had to replace
:
$query = Statjiblog::find()->with('categorys');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question