Bake\Shell\Task\ModelTask::getCounterCache PHP Method

getCounterCache() public method

Get CounterCaches
public getCounterCache ( Table $model ) : array
$model Cake\ORM\Table The table to get counter cache fields for.
return array CounterCache configurations
    public function getCounterCache($model)
    {
        $belongsTo = $this->findBelongsTo($model, ['belongsTo' => []]);
        $counterCache = [];
        foreach ($belongsTo['belongsTo'] as $otherTable) {
            $otherAlias = $otherTable['alias'];
            $otherModel = $this->getTableObject($this->_camelize($otherAlias), Inflector::underscore($otherAlias));
            try {
                $otherSchema = $otherModel->schema();
            } catch (\Cake\Database\Exception $e) {
                continue;
            }
            $otherFields = $otherSchema->columns();
            $alias = $model->alias();
            $field = Inflector::singularize(Inflector::underscore($alias)) . '_count';
            if (in_array($field, $otherFields, true)) {
                $counterCache[] = "'{$otherAlias}' => ['{$field}']";
            }
        }
        return $counterCache;
    }