Domanda

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?

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top