Cakephp 3.x CounterCache on the same model 

I have a model in a CakePHP app where I refer to the parents.
The tree behaviour seems to be an overkill for this purpose, but I needed the children's count for each random node. (For feeding this to a jstree widget through AJAX.)

My model was called ItemTypes and until I have not added the className => __CLASS__ to the belongsTo association the generated SQL query tried to refer to the parent_item_types which does not exists.


public function initialize(array $config)
{
parent::initialize($config);

$this->table('item_types';);
$this->displayField('name';);
$this->primaryKey('id';);

$this->addBehavior('Timestamp';);

$this->belongsTo('ParentItemTypes', [
'foreignKey' => 'parent_item_type_id',
'class' => 'ItemTypes',
'className' => __CLASS__
]);

$this->addBehavior(
'CounterCache',
[
'ParentItemTypes' => [
'child_item_type_count' => function ($event, $entity, $table) {
return $this->find('all', ['conditions' => ['ItemTypes.parent_item_type_id' => $entity->parent_item_type_id]])->count();
}
],
]
);
}


Many kudos to jose_zap!
[ hozzászólás ] ( 8 megtekintés ) [ 0 trackbackek ] permalink ( 3 / 2045 )

<< <Előző | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Következő> >>

 
számláló