Question

Working on my first CakePHP (2.x) project. I have Users that have many Clients. The baked view for the client form includes a select box to choose the user the client is associated to. I would like to remove this field from the form and have it automatically set to the user that created the client. I know I probably need to do something in my Client model's beforeSave() hook. What code do I put in that method to associate the newly created client with the user that is submitting the create form?

Was it helpful?

Solution

In the action that the form is submitting to, just add the user data in before you run the save(). Something like this:

//...
$postData['Client']['user_id'] = $this->Auth->user('id');
$this->Client->save($postData);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top