I’m having a problem adding a ticket to whoever is logged in. With cakephp 1.3 this line works perfectly fine: $this->data['Ticket']['user_id'] = $this->Auth->user(); Here’s the complete add action:
public function add() {
if ($this->request->is('post')) {
$this->Ticket->create();
$this->data['Ticket']['user_id'] = $this->Auth->user();
if ($this->Ticket->save($this->request->data)) {
$this->Session->setFlash(__('The ticket has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The ticket could not be saved. Please, try again.'));
}
}
$users = $this->Ticket->User->find('list');
$this->set(compact('users'));
}
Not even the code $this->Session->read('user_id'); works now. Well, either of the two adds the ticket but the user_id is blank. Any help would be greatly appreciated.
$this->Auth->user() returns an array with various fields about the user. I think you should replace line 4 with:
Or, you could assign the results to a variable first and then attach to the ticket:
EDIT
I see what’s happening now. You are making edits to an object and then saving the request data…so your user id never gets passed to the save.
Try this:
}