添加产品CakePHP时添加计数器
在我的表类别中,我有一个名为count的行,我希望在每次创建新产品时更新该行,但我对这个cakephp是新手,我不知道如何同时使用2个控制器 它是“我的产品”控制器中的外接程序:(默认为使用烘焙创建外接程序)添加产品CakePHP时添加计数器,php,cakephp,Php,Cakephp,在我的表类别中,我有一个名为count的行,我希望在每次创建新产品时更新该行,但我对这个cakephp是新手,我不知道如何同时使用2个控制器 它是“我的产品”控制器中的外接程序:(默认为使用烘焙创建外接程序) 每个产品都有一个相关的类别。请查看 您所要做的就是将它附加到您的模型上,并告诉它要存储计数的列的名称(我假设这里称为product\u count) 查看中的计数器缓存行为 您所要做的就是将它附加到您的模型上,并告诉它要存储计数的列的名称(我假设这里称为product\u count)
每个产品都有一个相关的类别。请查看 您所要做的就是将它附加到您的模型上,并告诉它要存储计数的列的名称(我假设这里称为
product\u count
)
查看中的
计数器缓存
行为
您所要做的就是将它附加到您的模型上,并告诉它要存储计数的列的名称(我假设这里称为product\u count
)
public function add()
{
$product = $this->Products->newEntity();
if ($this->request->is('post')) {
$product = $this->Products->patchEntity($product, $this->request->getData());
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$categorys = $this->Products->Categorys->find('list', ['limit' => 200]);
$this->set(compact('product', 'categorys'));
$this->set('_serialize', ['product']);
}
class CategoriesTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('CounterCache', [
'Products' => ['product_count']
]);
}
}