2012-02-17 10 views
1

私は "クーポン"コントローラを持っています。インデックス()関数は、各カテゴリにクーポンの一握りをつかんで(つまり)それらを表示:CakePHP: "ビュー"カウントを維持する

public function index() { 
    $showPerPage = 4; 

    $this->Coupon->recursive = 0; 

    $this->set('restaurants', $this->Coupon->findAllBycategory_id('1', '', '', $showPerPage)); 
    $this->set('entertainment', $this->Coupon->findAllBycategory_id('2', '', '', $showPerPage)); 
    $this->set('spas', $this->Coupon->findAllBycategory_id('3', '', '', $showPerPage)); 
    $this->set('services', $this->Coupon->findAllBycategory_id('4', '', '', $showPerPage)); 
    $this->set('hotels', $this->Coupon->findAllBycategory_id('5', '', '', $showPerPage)); 
    $this->set('retail', $this->Coupon->findAllBycategory_id('6', '', '', $showPerPage)); 
} 

各配列のセット(レストラン、entertinamnent、温泉、など)のために、私はを通じて各クーポンをフィルタリングします"mini_views"カウンタを1に増やします。私はモデルで "increaseMiniView($ id)"という関数を作成しました。これを処理する最善の方法は何ですか?私はそれぞれのことをちょうど前向きにすることができると知っていますが、良い方法があるかどうかは分かりませんでした(私はコードをビューに置くこともできましたが、それも最善の方法です)。

答えて

1

$restaurants = $this->Coupon->findAllBycategory_id('1', '', '', $showPerPage); 
foreach($restaurants['Coupon'] as $restaurant) 
    $couponId[] = $restaurant['id']; 

$entertainments = $this->Coupon->findAllBycategory_id('2', '', '', $showPerPage); 
foreach($entertainments['Coupon'] as $entertainment) 
    $couponId[] = $entertainment['id'];  

// ... repeat for all queries 

$this->Coupon->updateAll(
    array('Coupon.count' => 'Coupon.count+1'), 
    array('Coupon.id' => $couponId) 
); 

$this->set(compact('restaurants', 'entertainments')); 

Complex Find ConditionsupdateAll:あなたはこのような何かを行うことができます。

すべてのクーポンIDを配列に格納し、必要なフィールドを更新できるupdateAllメソッドにトラフを渡します。

幸運を祈る!

+1

正確に。また、ループを 'Set :: extract'に委譲することもできます。 – bfavaretto

関連する問題