Search code examples
validationcakephpmodeloncreateonupdate

cakePHP - creating new user account, several problems


I have two tables, users and tokens.
Each user have a activated field and each token have the {id, token, user_id, created} fields.

The way the app should work is: On the creation, the app will -

  1. make sure that the activated field is empty (to avoid manipulations to the submitted data).
  2. a token will be created in the tokens table.

On update, the app will -

  1. NOT create a new token.
  2. NOT allow an update of any kind to the activated field.
  3. check if a new email has been submitted, and if so: will create a new token and set the activated field to false.

I know how to activate the account through the controller and how to setup the router for that.
What I need is mainly the model configuration.
For example: I think that the token creation should be done in the afterSave method, so - how do I determine if the method is called by an update or by a create operation?

Thanks for any help


Solution

  • yossi you can also specify the fields that should be saved from the form though - a whitelist of fields it is ok to save in you $this->save() call. That way you can stop a hacker passing an ID in the request, and you should just set it in the controller yourself then with $this->Token->id = whatever you have, I would personally use saveField ('activated) in conjunction with this (just saves a single field!). Fat models is best if you can but get it working first then refactor it if you have got stuck. Better than wasting lots of time writing perfect first time.