0

I am trying to get the count of Comments for each Shop, and am really stuck on this step.
Is it possible to do it, and how?

public function display()
    {


        $this->loadModel('Shops');
        $shoplist=$this->Shops->find('all')->order(['title' => 'DESC', 'id' => 'ASC'])->contain([
            'Comments' => function ($q) {
                return $q->select('id')->where(['published' => 1])->count();
            }]);;

             $this->set('shoplist', $shoplist );
    }
Sk93
  • 3,676
  • 3
  • 37
  • 67

1 Answers1

0

What you need is CounterCache Behavior

http://book.cakephp.org/3.0/en/orm/behaviors/counter-cache.html

Salines
  • 5,674
  • 3
  • 25
  • 50