public function beforeFind(Event $event, $query, $options, $primary)
    {
        if ($this->_enabled && $query->count() > 0) {
            $pk = $this->_table->primaryKey();
            $tableAlias = Inflector::underscore($this->_table->alias());

            $query->contain([
                'Comments' => function ($query) {
                    return $query->find('threaded')
                        ->contain(['Users'])
                        ->order($this->config('order'));
                },
            ]);

            if ($this->config('count') ||
                (isset($options['comments_count']) && $options['comments_count'] === true)
            ) {
                $query->formatResults(function ($results) use ($pk, $tableAlias) {
                    return $results->map(function ($entity) use ($pk, $tableAlias) {
                        $entityId = $entity->{$pk};
                        $count = TableRegistry::get('Comment.Comments')->find()
                            ->where(['entity_id' => $entityId, 'table_alias' => $tableAlias])
                            ->count();
                        $entity->set('comments_count', $count);

                        return $entity;
                    });
                });
            }
        }
    }
public function beforeFind(Event $event, $query, $options, $primary)
    {
        if ($this->_enabled && $query->count() > 0) {
            $pk = $this->_table->primaryKey();
            $tableAlias = Inflector::underscore($this->_table->alias());

            $query->contain([
                'Comments' => function ($query) {
                    return $query->find('threaded')
                        ->contain(['Users'])
                        ->order($this->config('order'));
                },
            ]);

            if ($this->config('count') ||
                (isset($options['comments_count']) && $options['comments_count'] === true)
            ) {
                $query->formatResults(function ($results) use ($pk, $tableAlias) {
                    return $results->map(function ($entity) use ($pk, $tableAlias) {
                        $entityId = $entity->{$pk};
                        $count = TableRegistry::get('Comment.Comments')->find()
                            ->where(['entity_id' => $entityId, 'table_alias' => $tableAlias])
                            ->count();
                        $entity->set('comments_count', $count);

                        return $entity;
                    });
                });
            }
        }
    }
public function beforeFind ( Event $ event , $ query , $ options , $ primary ) { if ( $ this -> _enabled && $ query -> count ( ) > 0 ) { $ pk = $ this -> _table -> primaryKey ( ) ; $ tableAlias = Inflector :: underscore ( $ this -> _table -> alias ( ) ) ; $ query -> contain ( [ 'Comments' => function ( $ query ) { return $ query -> find ( 'threaded' ) -> contain ( [ 'Users' ] ) -> order ( $ this -> config ( 'order' ) ) ; } , ] ) ; if ( $ this -> config ( 'count' ) || ( isset ( $ options [ 'comments_count' ] ) && $ options [ 'comments_count' ] === true ) ) { $ query -> formatResults ( function ( $ results ) use ( $ pk , $ tableAlias ) { return $ results -> map ( function ( $ entity ) use ( $ pk , $ tableAlias ) { $ entityId = $ entity -> { $ pk } ; $ count = TableRegistry :: get ( 'Comment.Comments' ) -> find ( ) -> where ( [ 'entity_id' => $ entityId , 'table_alias' => $ tableAlias ] ) -> count ( ) ; $ entity -> set ( 'comments_count' , $ count ) ; return $ entity ; } ) ; } ) ; } } }
Attaches comments to each entity on find operation.

@param \Cake\Event\Event $event The event that was triggered
@param \Cake\ORM\Query $query The query object
@param array $options Additional options as an array
@param bool $primary Whether is find is a primary query or not
@return void
Attaches comments to each entity on find operation .