2009-10-14 5 views
0

연락처의 ID가 포함 된 링크를 클릭 할 때마다 contact_status_id 필드의 연락처 테이블에서 상태 번호를 토글 링합니다. 페이지에 오류는 표시되지 않지만 조치로 번호가 변경되지는 않습니다. 아직 구현 된 유효성 검사가 없습니다.CakePHP, 편집 작업에서 문제를 발견 할 수 없음

아마도 신선한 눈이 문제를 일으킬 수 있습니까?

function inbox_toggle_number_status($id=null) 
{ 
    //Call from the inbox when the number is clicked and status toggled. 

    $this->User->Contact->id = $id; 

    if (!empty($id)) 
    { 

     $current_status = $this->User->Contact->find('first', array('conditions' => array('id' => $id))); 

     if ($current_status['Contact']['contact_status_id'] == '1'): 
      $this->User->Contact->saveField('contact_status_id', '2'); 
      exit(); 

     elseif ($current_status['Contact']['contact_status_id'] == '2'): 
      $this->User->Contact->saveField('contact_status_id', '3'); 
      exit(); 

     elseif ($current_status['Contact']['contact_status_id'] == '3'): 
      $this->User->Contact->saveField('contact_status_id', '2'); 
      exit(); 

     else: 
      exit(); 

     endif; 
    } 
} 
+2

하나의 문제가 보이고 코드가 제대로 들여 쓰기가 어렵고 한 줄에 여러 개의 명령문이있어 읽기가 어려워 문제를 발견하기 어렵습니다. , 그리고 유지하기가 어렵다. 따라서 이것이 비즈니스의 첫 번째 주문이어야하며 코드를 읽을 수있게하십시오. –

+0

맞아, 나는 조금 정돈했다. – ondrobaco

답변

1
function inbox_toggle_number_status($id = null) 
{ 
    if(!$id) 
    { 
     $this->Session->setFlash('no id'); 
     $this->redirect(array('action' => 'index')); 
    } 

    $this->User->Contact->id = $id; 
    $current_status = $this->User->Contact->read(null, $id); 

    switch($current_status['Contact']['contact_status_id']) 
    {     
     case 1: 
      $this->User->Contact->saveField('contact_status_id', 2); 
      break; 

     case 2: 
      $this->User->Contact->saveField('contact_status_id', 3); 
      break; 

     case 3: 
      //should yu not go back to 1? 
      $this->User->Contact->saveField('contact_status_id', 2); 
      break; 
    } // switch 
} 
0

나는 케이크에서 몇 가지 저장 방법을 사용한 적이별로 없습니다. 이런 이유로, 저는 거의 항상 Model :: save (array())를 사용합니다. id와 업데이트 할 필드가 모두있는 배열을 사용합니다. 이 경로를 이동하고 싶다면, 당신은 할 수 :

if(!empty($id)) { 
    $arr = array('Contact' => array('id' => $id)); 

    $current_status = $this->User->Contact->find('first', array('conditions' => array('id' => $id))); 

    if ($current_status['Contact']['contact_status_id'] == '1'): 
     $arr['Contact']['contact_status_id'] = 2; 
    <other conditional clauses here> 


    $this->User->Contact->save($arr); 
    exit(); 
} 

당신이 당신의 경로를 계속하려는 경우에 디버그 레벨 2를 설정하십시오하고 스크립트도에 값을 저장하려고하면 알려 db (페이지 하단에 SQL 덤프).

0

나는 아이디어가 있었다; 이 줄을 보시겠습니까?

$this->User->Contact->id = $id; 

이 ID의 설정은이 설정 한 후 실행 쿼리 지속될 수도

그래서

그냥 saveField 호출을 포함하는 경우 문 앞에 다시 설정해야 할 수도 있습니다 .

그러나 find (...) '조건'배열의 일부로 $ id가 설정되어 있기 때문에 find (...) 호출 전에 실제로 필요하지는 않습니다.

솔루션

가 오는 setField (을 설정할 수 있도록, 발견 (...) 후 라인에 $ this-> 사용자 -> Contact-> 아이디 = $ id를 이동하려고합니다. ..) 호출. 또한

대신를 사용

! 빈 (...)는 $ ID가 유효한 경우 실행 할 코드 뒤에

if(!isset($id)){ 
    $this->redirect(/*some url with an error message*/); 
} 

와 기능을 시작하려고

관련 문제