2016-10-11 3 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']); 
} 
関連する問題