Mariusz Adamczewski
Mariusz Adamczewski

Reputation: 107

Saving single data into a database - CakePHP

I tried to do on all things I think and I dont know how to do it. I have read the book of Cake...

I dont know how to add this value in 1 column in my database. When I'm adding 'Task' I want to add my 'name' in column 'created_by'.

Code from Add Task:

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']);
}

When I will write

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

I have got my name which is 'Mariusz'. Now I want to add it into column 'created_by'. I think I need to change this line:

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

But I dont know how. In book of Cake there is information

$articles->patchEntity($article, $this->request->data(), [ 'validate' => 'custom', 'associated' => ['Tags', 'Comments.Users' => ['validate' => 'signup']] ]);

But I dont think its good.

I have created the relation User -> has_many -> Task Task -> belongs_To -> User

Upvotes: 0

Views: 60

Answers (1)

Er.KT
Er.KT

Reputation: 2860

Please can you try following code :

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

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

Upvotes: 1

Related Questions