CakePHP - creating a new user account, several problems

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

The way the application works: When you create the application will be -

  • make sure that the activated field is empty (to avoid manipulation of the presented data).
  • a token will be created in the tokens table.

When updating the application will be

  • DO NOT create a new token.
  • DO NOT allow any type of update in the activated field.
  • check if a new letter has been sent, and if so: create a new token and set the activated field to false.

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

Thanks for any help

0
source share
2 answers

yossi, you can also specify the fields that should be saved from the form, but a white list of fields that can be saved in this call $ this-> save (). That way, you can stop the hacker passing the identifier in the request, and you just have to install it in the controller yourself, and then using $ this-> Token-> id = no matter what you have, I would personally use saveField ("activated") with this (just saves one field!). Bold models are best if you can, but start working first and then reorganize them if you're stuck. Better than spending a lot of time writing for the first time.

+1
source

You doubt it. If you have a default value for the field, then why not set it in the database and not do something in the aftersave? If you need to do something that needs to be done only in certain circumstances, write a custom method in your model to perform the tasks you want to create or update.

Edit

So, if your record has an identifier, then you know that it exists in the database. So, the simple thing to do (in any method) is to check if the model has an identifier field and that it is not empty. If it is empty, then you know that you are creating a record and you can complete the x task. If this is not so, then complete the task.

 if(isset($modelData['ModelName']['id']) && !empty($modelData['ModelName']['id'])){ //This is an update } else { //This is a new record } 
+1
source

Source: https://habr.com/ru/post/896250/


All Articles