Controller:

public function actionIndex() { $category = Category::getCategoryimg(); $pages = new Pagination(['totalCount' => $category->count(), 'pageSize' => 1]); $models = $category->offset($pages->offset) ->limit($pages->limit) ->all(); return $this->render('index', [ 'category'=> $category, 'models' => $models, 'pages' => $pages, ]); } 

Model:

 public static function getCategoryimg() { $data = Category::find() ->from('category') ->where(['parent_id' => null]) ->all(); return $data; } 

Mistake:

Call to a member function count () on array

Question:

Why does count() react to all() ?


Representation:

 <?php use yii\widgets\LinkPager; ?> <div class="content-index"> <div class="row"> <?php foreach ($category as $cat): ?> <div class="col-md-3 col-sm-4 col-xs-6"> <div class="thumbnail"> <a href="/category/album?id=<?= $cat->id?>"> <img src="<?= '/backend/web/'.$cat->category_image ?>" alt="image" width="500" height="500" class="img-rounded"> </a> <h4 align="center"><?= $cat->name?></h4> <?php if(!($cat->parent_id == null)) { ?> <h5 align="center">Category: <?= $cat->category->name ?></h5> <? } ?> <a href="/category/view?id=<?= $cat->id?>">Детальніше..</a> </div> </div> <?php endforeach; ?> </div> </div> <?php echo LinkPager::widget([ 'pagination' => $pages, ]); 

    1 answer 1

    Make an empty PHP file like this:

     []->count(); 

    and you get the same error:

    Call to a member function count () on array


    The thing is that when you call such methods as: all() , one() , count() , etc ; then you directly query the database, and in your case you just need to return the object with the prepared request. In other words, remove all() from getCategoryimg() all() .


    Documentation

    Documentation

    • Request such that without -> all does not issue what is needed. How can I redo a request? - Vlad
    • @Vlad, what does it mean "does not give what is needed"? Are you all() where you need removed? - Roman Grinyov
    • c2n.me/3Ahc9k0 - view from -> all, c2n.me/3AhcbDn - view without -> all - Vlad
    • edited the question - Vlad
    • one
      @Vlad, I gave the link to the documentation in the answer, where it can be seen: $query = Article::find()->where(['status' => 1]); ... - Roman Grinyov