CakePHP:保持“查看”计数

时间:2012-02-17 18:43:53

标签: cakephp

我有一个“优惠券”控制器。 index()函数在每个类别中抓取少量优惠券并显示它们(即):

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

对于每个阵列集(餐馆,娱乐场所,水疗中心等),我想通过过滤每个优惠券并将其“mini_views”计数器增加到1.我在模型中创建了一个函数称为“increaseMiniView($ id)”。处理这个问题的最佳方法是什么?我知道我可以通过每一个来解决,但我不知道是否有更好的方法(我也可以将代码放在视图中,但我知道这也是最好的方法)..

1 个答案:

答案 0 :(得分: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方法,您可以在其中更新所需的任何字段。

祝你好运!