2017-10-30 14 views
0

私のテーブルのカテゴリには、新しい製品が作成されるたびに更新したいcountという行がありますが、このcakephpを初めて使用していて、2コントローラを同時に使用することができます。製品を追加するときにカウンタを追加する

それは私の製品コントローラに追加します。(デフォルトは焼くと作成追加される)

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']); 
    } 

各製品は、関連するカテゴリがあります。

答えて

2

である(私はここで前提とあなたがしなければならないのは、あなたのモデルに添付し、それをあなたが、カウントを保存する列の名前を教えているCookbook

CounterCache行動を見てみましょうproduct_count

class CategoriesTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     $this->addBehavior('CounterCache', [ 
      'Products' => ['product_count'] 
     ]); 
    } 
} 
関連する問題