2014-12-30 7 views
0
세트로 테이블을 업데이트 할 수 없습니다

안녕하세요 모두 cakephp의 데이터베이스 테이블을 업데이트하는 데 문제가 있습니다 ... 로그인 한 사용자가 자신의 개인 정보를보고 업데이트 할 수있는 프로필 페이지가 있습니다. 여기 내 솔루션 (그래 난 ... 그것은 최고의하지 알고) cakephp가

if($this->request->is('post')) { 
      $data = $this->request->data['user-profile']; 
      // $uid = $this->Session->read("WebUser.id"); 
      $uid = 1; 
      $user_data = $this->WebUser->find('first', array('conditions'=>array('id'=>$uid)));    

      $updated_fields = ''; 
      foreach($data as $key => $value) { 
       if($key != 'state'){ 
        if(empty($value)) {                
         $this->Session->setFlash("Please fill in all the required fields");      
         return; 
        } 
       }    
       if($user_data['WebUser'][$key] != $value) { 
        if($key == 'email') { 
         $mail_check = $this->WebUser->find('first', array('conditions'=>array('email'=>$value, 'id !='=>$uid))); 
         if($mail_check) { 
          $this->Session->setFlash("This e-mail is already registered"); 
          return; 
         } 
        } 
        $updated_fields .= "'".$key."'=>'".$value."',"; 
       }    
      } 
      if($updated_fields != '') { 
       $updated_fields .= "'modified'=>'".date("Y-m-d H:i:s")."'"; 
       $this->WebUser->read(null, $uid); 

       $this->WebUser->set(array('first_name'=>'John','modified'=>'2014-12-30 10:53:00')); 

       if($this->WebUser->save()) { 
        $this->printr($this->data); 
        $this->WebUser->save(); 
        $this->Session->setFlash("Success : Profile data is now modified", array("class"=>"success_msg")); 
       } else { 
        $this->Session->setFlash("Error : Data modifying isn't complete. Please try agin!"); 
       } 
      } 
      return; 
     } 

그래서이 코드는 데이터베이스에서 사용자 정보를 가져오고 해당 필드 마녀를 찾는 것은 프로필 페이지에 편집이다. 문제는 내가 다시 허위로주고 저장하지 않은 데이터를 저장하려고 할 때입니다. Some1은 이것을위한 해결책이 있습니까? 나는 아직 찾지 못했다. (추신 : 나는 cakePHP : D로 새것임)

+0

사용자가이 양식을 제출할 때 오류 메시지가 인쇄됩니까? 또한보기 코드를 첨부 해주십시오. 나는 edit.ctp 또는 뭔가라고 생각한다. – bcesars

답변

0

시도해보십시오. $ this-> WebUser-> validationErrors; 다른 부분에 을 입력하고 유효성 검사 오류가 있는지 여부를 확인하십시오. 이처럼

:

if($this->WebUser->save()) { 
       $this->printr($this->data); 
       $this->WebUser->save(); 
       $this->Session->setFlash("Success : Profile data is now modified", array("class"=>"success_msg")); 
      } else { 
       echo "<pre>";print_r($this->WebUser->validationErrors);die(); 
       $this->Session->setFlash("Error : Data modifying isn't complete. Please try agin!"); 
      } 
0

iddirectly을 설정하려고 했습니까?

$this->WebUser->id = $uid; 

또한 CakePHP는 트랜잭션 후 테이블의 modified 필드를 자동으로 업데이트합니다. 따라서 직접 설정할 필요는 없습니다 (다른 날짜 값을 원한다면 할 수 있습니다).

인증 정보. CakePHP에서 Data Validation에 대한 정보를 읽는 것이 좋습니다. 유효성 검사가 좋지 않아 모델에서 처리 할 수 ​​있기 때문입니다.

또한. Model::$validationErrors을 사용하여 모델에서 유효성 검사 오류를 얻을 수 있습니다.

debug($this->Recipe->validationErrors); 
0

이 코드의 문제점은 게시 된 데이터를 저장하지 않으려는 것입니다 (디버그 ($ this-> WebUser-> save)가 허위로 허락했습니다.). 결국이 코드가 작동하지 않는 해결책을 찾지 못했지만 결론은 사용자 테이블을 더 많이 사용할 필요가 없다는 것입니다. 이제 사용자 및 그룹 테이블이 생겼고 다른 유형의 사용자가 그룹 (관리, 사용자, ...)에 연결되었습니다.

관련 문제