2016-10-11 2 views
0

CakePHP에서 사용자 및 작업으로 약간의 응용 프로그램을 만들었습니다. 삭제하려는 사용자를 삭제하려는 경우 'status'열이 '1'에서 '0'인 데이터베이스의 값만 변경하십시오. 나는 그것을 어떻게하는지 모른다.UsersController에서 데이터베이스로 데이터 저장 - CakePHP

사용자 컨트롤러의 코드 (삭제) :

public function delete($id = null) 
{ 
    $this->request->allowMethod(['post', 'delete']); 
    $user = $this->Users->get($id); 
    if ($user['status'] == '1') { 
     //Here should be code about change value 
     $this->Flash->success(__('The user has been deleted.')); 
    } else { 
     $this->Flash->error(__('The user could not be deleted. Please, try again.')); 
    } 

    return $this->redirect(['action' => 'index']); 
} 

어떤 아이디어?

+1

북을 읽으십시오. http://book.cakephp.org/3.0/en/orm/saving-data.html 이것은 프레임 워크의 기본에 대해 잘 모르는 것처럼 들리므로 시작하기 위해 자습서를 할 것을 권장합니다. – burzum

답변

0

올바른 명명 규칙에 따라 모델을 만든 경우 아래 코드를 시도 할 수 있습니다.

public function delete($id = null) { 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     if ($this->User->saveField('status', 0);) { 
      $this->Flash->success(__('User deleted')); 
      return $this->redirect(array('action' => 'index')); 
     } 
     $this->Flash->error(__('User was not deleted')); 
     return $this->redirect(array('action' => 'index')); 
    } 
+0

대단히 감사합니다! –

0
public function delete($id = null) 
{ 
    $this->request->allowMethod(['post', 'delete']); 
    $user = $this->Users->get($id); 
    if ($user->status == 1) { 
     $user->status = 0; 
     $this->Users->save($user); 
     $this->Flash->success(__('The user has been deleted.')); 
    } else { 
     $this->Flash->error(__('The user could not be deleted. Please, try again.')); 
    } 

    return $this->redirect(['action' => 'index']); 
} 
관련 문제