1

I am playing with cakephp 4. I have three table. This is belongsToMany Relation.

1. me_article
   me_article.id,me_article.slug,me_article.title
   'foreignKey' => 'me_category_id',
   'targetForeignKey' => 'me_artcle_id',
2. me_category
   me_category.id,me_category.name,me_category.slug
   'foreignKey' => 'me_article_id',
   'targetForeignKey' => 'me_category_id',
3. me_article_me_category (joinTable)
   me_article_id,me_category_id

I get related articles for single category using this simple code :

$meCategory =  $this->MeCategory->get($article->id, 
        [
            'contain' => ['MeArticle'],
        ]);

The question is : How to paginate $meCategory?

I am doing this, and still getting error

$artikel = $this->MeArticle
      ->findBySlug($slug)
      ->contain(['MeCategory'])
      ->first();

      $categoriesQuery = $this->MeArticle->MeCategory
      ->find()
      ->innerJoinWith('MeArtikel', function (\Cake\ORM\Query $query) use ($artikel) {
        return $query->where([
            'MeArticle.id' => $artikel->id,
        ]);
      })
      ->group('MeCategory.id');

      $paginationOptions = [
      'limit' => 6,
      'order' => [
        'MeCategory.nama' => 'ASC'
    ]
    ];

    $categories = $this->paginate($categoriesQuery, $paginationOptions);

The error : Expression MeArticle.id is missing operator (IS, IS NOT) with null value

  • **https://stackoverflow.com/questions/43901181/how-to-paginate-associated-records** – ndm Jul 16 '22 at 10:56
  • I am getting error : ```Expression MeArticle.id is missing operator (IS, IS NOT) with null value.``` – Achmad Muqorobin Jul 17 '22 at 04:33
  • That would indicate that `$artikel->id` is `null`. Debug the entity and its value (note that `first()` can return `null`, you should rather use `firstOrFail()` or manually check whether a record was found), and figure if that is the case, and if it _is_, whether that is actually a valid thing to happen (would be weird, assuming `id` is the primary key). – ndm Jul 18 '22 at 14:40
  • The problem is solved now, thanks the clue. – Achmad Muqorobin Jul 18 '22 at 15:05
  • @ndm, like you say : ```The paginator doesn't support paginating associations, you'll have to read the associated records manually in a separate query, and paginate that one, ``` , and I am not using your full code example , I use different way to join the table. – Achmad Muqorobin Jul 20 '22 at 14:47

0 Answers0