2012-02-17 4 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, 온천 등), I는 쿠폰을 필터링 할 "mini_views"카운터를 1로 늘리십시오. "increaseMiniView ($ id)"라는 모델에서 함수를 만들었습니다. 이것을 처리하는 가장 좋은 방법은 무엇입니까? 나는 각자를 통해 단지 foreach를 할 수 있다는 것을 알고 있지만 더 좋은 방법이 있는지 나는 알지 못했다. (나는 코드를보기에 넣을 수도 있지만, 그 중 가장 좋은 방법이다).

답변

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