2016-10-12 6 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); 

しかし私は知らない。ケーキの本の中で情報

あり

$ articles-> patchEntity($記事、ます$ this->要求 - >データ()、[ '検証' => 'カスタム'、 '関連します' => ['Tags'、 'C​​omments.Users' => ['validate' => 'signup']] ]);

しかし、私はその良いとは思わない。

私は関係 ユーザー作成した

- > has_manyの - >タスク タスク - > BELONGS_TO - >ユーザコード以下

答えて

1

あなたが試すことができますしてください。

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

if ($this->Tasks->save($task)) { 
関連する問題