2016-10-12 49 views
0

我試圖做所有我認爲的事情,我不知道該怎麼做。我已閱讀蛋糕書...將單個數據保存到數據庫中 - CakePHP

我不知道如何在我的數據庫的1列中添加此值。 當我添加「任務」時,我想在'created_by'列中添加我的'姓名'。從添加任務

代碼:

public function add() 
{ 
    $task = $this->Tasks->newEntity(); 
    if ($this->request->is('post')) { 
     $task = $this->Tasks->patchEntity($task, $this->request->data); 

     if ($this->Tasks->save($task)) { 
      $this->Flash->success(__('The task has been saved.')); 

      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error(__('The task could not be saved. Please, try again.')); 
     } 
    } 
    $users = $this->Tasks->Users->find('list', ['limit' => 200]); 
    $this->set(compact('task', 'users')); 
    $this->set('_serialize', ['task']); 
} 

當我會寫

echo $this->Auth->user('name'); 
die(); 

我有我的名字是 '馬裏烏什'。 現在我想將它添加到'created_by'列中。我想我需要改變這一行:

$task = $this->Tasks->patchEntity($task, $this->request->data); 

但我不知道如何。在蛋糕的書有信息

$物品─> patchEntity($製品,$這 - >請求 - >數據(),[ '驗證'=> '關聯' '自定義', => ['Tags','Comments.Users'=> ['validate'=>'註冊']] ]);

但我不認爲它很好。

我創建的關係 用戶 - >的has_many - >任務 任務 - > belongs_to的 - >用戶

回答

1

請你可以試試下面的代碼:

$data['name']=$this->Auth->user('name'); 
$task = $this->Tasks->patchEntity($task, $data); 

if ($this->Tasks->save($task)) {