2012-01-07 3 views
1

내 문제는 다음과 같습니다 :CakePHP 증가 값

사람들이 기념일 로고와 ​​같은 하나 이상의 이벤트를 선택할 수있는 투표 응용 프로그램을 만들고 싶습니다. 이를 위해 나는 vote라는 함수를 설정했습니다. 보기에서 이벤트 체크 박스를 사용하여 선택할 수 있습니다. 모델은 투표 및 Groupevent입니다. 여론 조사에는 많은 그룹전이 있습니다. 내 문제는 updateAll()으로 전화하면 관련 Groupevents의 모든 값이 증가합니다. 여기

내 코드입니다 :

보기 :

echo $this->Form->create('Poll', array('action' => 'vote')); 

for($i=0; $i<$count; $i++){ 
    echo $this->Form->input('Groupevent.'.$i.'.votes', array('type'=>'checkbox', 
     'label'=>$this->Groupevent['startTime'])); 
    echo $this->Form->input('Groupevent.'.$i.'.id', array('type'=>'hidden')); 
} 
echo $this->Form->input('id', array('type' => 'hidden')); 
echo $this->Form->end('vote'); 

컨트롤러 기능 :

function vote($id=null){ 
    $this->Poll->id = $id; 
    if ($this->request->is('get')) { 
     $this->request->data = $this->Poll->read(); 
     $this->set('count', $this->Poll->Groupevent->find('count', 
      array('conditions'=>array('Groupevent.poll_id'=>$this->Poll->id))));            
    } else { 
     unset($this->Poll->Groupevent->validate['poll_id']);    
     if ($this->Poll->Groupevent->updateAll(
       array('Groupevent.votes'=>'Groupevent.votes+1'), 
       array('Groupevent.poll_id'=>$this->Poll->id))) 
     {        
      $this->Session->setFlash('Your poll has been updated.'); 
      $this->redirect(array('action' => 'edit', $id)); 
     } else { 
      $this->Session->setFlash('Unable to update your poll.'); 
     } 
    } 
} 

난 그냥 체크 값이 증가 얻을 수 있도록이 작동 할 수 있는가? 사전에

감사

편집 : 배열과 제안에 대한

감사합니다. 그러나 나는 그것이 작동하지 않는 특정 방법을 시도했다. 이 배열을 어떻게 만듭니 까?

답변

4
당신이 선택한 설문 조사에 할당 된 모든 Groupevents에 대한 updateAll 쿼리를 실행하는 것 같습니다

:

if ($this->Poll->Groupevent->updateAll(
       array('Groupevent.votes'=>'Groupevent.votes+1'), 
       array('Groupevent.poll_id'=>$this->Poll->id))) 
     { 
... 

당신은 확인 Groupevents의 ID의 배열을 생성하고 업데이트를 제한하는 조건을 추가 할 필요를 선택된 이벤트 :

if ($this->Poll->Groupevent->updateAll(
       array('Groupevent.votes'=>'Groupevent.votes+1'), 
       array('Groupevent.id IN' => $selectedEventsIds), 
       array('Groupevent.poll_id'=>$this->Poll->id))) 
     { 
... 
+0

안녕하세요, 감사이 있으면 알려주세요. 그러나이 데이터에서 배열을 만들 수는 없습니다. 나는 $ this-> data [ 'Groupevent']와 $ this-> data [ 'Groupevent'] [ 'id']와 다른 것들을 시도했지만 작동하지 않았다. – ulanBator

+0

제출 후 print_r ($ this-> data) 덤프를 붙여 주실 수 있습니까? – bbb

+0

나는 이것을 시도했지만 비어있다. – ulanBator

0

나는 UP 투표 또는 특정 주석 다운하기 내가 (jQuery를 통해) 사용자 수 있도록 어디 개발하고 응용 프로그램을 가지고있다. 이것이 내가 구현 한 방법이다. 기본적으로 Ajax/jQuery를 통해이 함수를 호출하고 전달 된 인수로 설명 만 증가시킵니다. 내 댓글 표에는 좋아요와 싫어요 2 개의 입력란이 있습니다. voteUp과 나도 꽤 비슷한 voteDown을 가지고있는 한 가지 예입니다.

function voteUp($id = null){ 

    $this->autoRender = false; 
    if($this->RequestHandler->isAjax()){ 
     $this->Comment->id = $id; 
     // Basically I get the value of liked(Field in Db) and increment it by 1 
     $this->Comment->saveField('liked',$this->Comment->field('liked')+1); 
    }  
    $newValue = $this->Comment->findById($id);  
    //pass this value back to the view so it is displayed 
    //using jQuery 
    return $newValue['Comment']['liked']; 
} 

더 이상 응답에 대한 질문

+2

많은 트래픽이 발생하는 사이트에서 간혹 서로 다른 데이터를 덮어 쓰며 누락 된 동시 요청을받을 수 있으므로주의해야합니다 (지불과 같은 중요한 상황의 경우 좋지 않습니다). 이 상황을 더 안전하게 처리해야하는'$ this-> Model-> updateAll ([ 'x'=> 'x + 1'), [ 'id'=> 1]);을 사용하는 것이 더 낫다. –

+0

이 조언을 주셔서 감사합니다 ... –