Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加产品CakePHP时添加计数器_Php_Cakephp - Fatal编程技术网

添加产品CakePHP时添加计数器

添加产品CakePHP时添加计数器,php,cakephp,Php,Cakephp,在我的表类别中,我有一个名为count的行,我希望在每次创建新产品时更新该行,但我对这个cakephp是新手,我不知道如何同时使用2个控制器 它是“我的产品”控制器中的外接程序:(默认为使用烘焙创建外接程序) 每个产品都有一个相关的类别。请查看 您所要做的就是将它附加到您的模型上,并告诉它要存储计数的列的名称(我假设这里称为product\u count) 查看中的计数器缓存行为 您所要做的就是将它附加到您的模型上,并告诉它要存储计数的列的名称(我假设这里称为product\u count)

在我的表类别中,我有一个名为count的行,我希望在每次创建新产品时更新该行,但我对这个cakephp是新手,我不知道如何同时使用2个控制器

它是“我的产品”控制器中的外接程序:(默认为使用烘焙创建外接程序)


每个产品都有一个相关的类别。

请查看

您所要做的就是将它附加到您的模型上,并告诉它要存储计数的列的名称(我假设这里称为
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']
        ]);
    }
}