Thursday, July 16, 2015

Re: CakePHP 3

Hi Rafael, 

$userid = $this->Auth->user('id');
$this->request->data['user_id'] = $userid;
$article = $this->Articles->patchEntity($article, $this->request->data);
if ($this->Articles->save($article)) {

That works for me, hope it helps! 
Cheers, 
Sajoscha


On Jul 16, 2015, at 3:59 PM, Rafael Queiroz <rafaelfqf@gmail.com> wrote:

Hi guys,

I need store the currently logged in user as a reference for the created register, the cakebook sample is:
public function add()  {      $article = $this->Articles->newEntity();      if ($this->request->is('post')) {          $article = $this->Articles->patchEntity($article, $this->request->data);          // Added this line          $article->user_id = $this->Auth->user('id');          // You could also do the following          //$newData = ['user_id' => $this->Auth->user('id')];          //$article = $this->Articles->patchEntity($article, $newData);          if ($this->Articles->save($article)) {              $this->Flash->success(__('Your article has been saved.'));              return $this->redirect(['action' => 'index']);          }          $this->Flash->error(__('Unable to add your article.'));      }      $this->set('article', $article);  }  
In old version,  2.x, i use beforeSave callbacks in Model and load AuthComponent, for save user id logged.

Any sugestion for Cake 3 ? Thanks.

--
Regards,

Rafael F. Queiroz

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: