src/Model/Behavior/TagBehavior.php
public function attachCounters()
    {
        $config = $this->getConfig();
        $tagsAlias = $config['tagsAlias'];
        $taggedAlias = $config['taggedAlias'];

        $taggedTable = $this->_table->{$taggedAlias};

        if (!$taggedTable->hasBehavior('CounterCache')) {
            $taggedTable->addBehavior('CounterCache');
        }

        $counterCache = $taggedTable->behaviors()->CounterCache;

        if (!$counterCache->getConfig($tagsAlias)) {
            $counterCache->setConfig($tagsAlias, $config['tagsCounter']);
        }

        if ($config['taggedCounter'] === false) {
            return;
        }

        foreach ($config['taggedCounter'] as $field => $o) {
            if (!$this->_table->hasField($field)) {
                throw new RuntimeException(sprintf(
                    'Field "%s" does not exist in table "%s"',
                    $field,
                    $this->_table->getTable()
                ));
            }
        }

        if (!$counterCache->getConfig($taggedAlias)) {
            $field = key($config['taggedCounter']);
            $config['taggedCounter']['tag_count']['conditions'] = [
                $taggedTable->aliasField($this->getConfig('fkTableField')) => $this->_table->getTable(),
            ];
            $counterCache->setConfig($this->_table->getAlias(), $config['taggedCounter']);
        }
    }
public function attachCounters()
    {
        $config = $this->getConfig();
        $tagsAlias = $config['tagsAlias'];
        $taggedAlias = $config['taggedAlias'];

        $taggedTable = $this->_table->{$taggedAlias};

        if (!$taggedTable->hasBehavior('CounterCache')) {
            $taggedTable->addBehavior('CounterCache');
        }

        $counterCache = $taggedTable->behaviors()->CounterCache;

        if (!$counterCache->getConfig($tagsAlias)) {
            $counterCache->setConfig($tagsAlias, $config['tagsCounter']);
        }

        if ($config['taggedCounter'] === false) {
            return;
        }

        foreach ($config['taggedCounter'] as $field => $o) {
            if (!$this->_table->hasField($field)) {
                throw new RuntimeException(sprintf(
                    'Field "%s" does not exist in table "%s"',
                    $field,
                    $this->_table->getTable()
                ));
            }
        }

        if (!$counterCache->getConfig($taggedAlias)) {
            $field = key($config['taggedCounter']);
            $config['taggedCounter']['tag_count']['conditions'] = [
                $taggedTable->aliasField($this->getConfig('fkTableField')) => $this->_table->getTable(),
            ];
            $counterCache->setConfig($this->_table->getAlias(), $config['taggedCounter']);
        }
    }
public function attachCounters ( ) { $ config = $ this -> getConfig ( ) ; $ tagsAlias = $ config [ 'tagsAlias' ] ; $ taggedAlias = $ config [ 'taggedAlias' ] ; $ taggedTable = $ this -> _table -> { $ taggedAlias } ; if ( ! $ taggedTable -> hasBehavior ( 'CounterCache' ) ) { $ taggedTable -> addBehavior ( 'CounterCache' ) ; } $ counterCache = $ taggedTable -> behaviors ( ) -> CounterCache ; if ( ! $ counterCache -> getConfig ( $ tagsAlias ) ) { $ counterCache -> setConfig ( $ tagsAlias , $ config [ 'tagsCounter' ] ) ; } if ( $ config [ 'taggedCounter' ] === false ) { return ; } foreach ( $ config [ 'taggedCounter' ] as $ field => $ o ) { if ( ! $ this -> _table -> hasField ( $ field ) ) { throw new RuntimeException ( sprintf ( 'Field "%s" does not exist in table "%s"' , $ field , $ this -> _table -> getTable ( ) ) ) ; } } if ( ! $ counterCache -> getConfig ( $ taggedAlias ) ) { $ field = key ( $ config [ 'taggedCounter' ] ) ; $ config [ 'taggedCounter' ] [ 'tag_count' ] [ 'conditions' ] = [ $ taggedTable -> aliasField ( $ this -> getConfig ( 'fkTableField' ) ) => $ this -> _table -> getTable ( ) , ] ; $ counterCache -> setConfig ( $ this -> _table -> getAlias ( ) , $ config [ 'taggedCounter' ] ) ; } }
Attaches the `CounterCache` behavior to the `Tagged` table to keep counts
on both the `Tags` and the tagged entities.

@return void
@throws \RuntimeException If configured counter cache field does not exist in table.
Attaches the CounterCache behavior to the Tagged table to keep counts on both the Tags and the tagged entities .