I want to prevent new registration when I update with CakePHP.

Asked 1 years ago, Updated 1 years ago, 100 views

Assumptions include the following tables

Create Table: CREATE TABLE `data`(
  `id`varchar(100) NOT NULL DEFAULT',
  `name`varchar(255)DEFAULT NULL,
  PRIMARY KEY (`id`)
ENGINE=InnoDB DEFAULT CHARSET=utf8

I would like to update this table by letting the user specify an id, name.
Simply execute the following save method

$this->data->save([]
    'id' = > 'User-entered ID',
    'name' = > 'User entered name'
]);

*Validation is not considered

If the ID exists, it will be updated, but if it does not exist, it will be "newly registered with that ID."
If the ID does not exist, I would like you to finish the process without doing anything, but would that be possible?

php cakephp

2022-09-30 21:15

2 Answers

The controller has a callback method called beforeSave.
http://book.cakephp.org/2.0/ja/models/callback-methods.html

With this, beforeSave() is processed when saving, and save() is performed when the return value is true.
In this beforeSave(),

public function beforeSave(){
    return!empty($this->find('count', array('conditions'=>array('id'=>$this->id)));
}

It would be better to confirm the existence of such things as save() if it exists, and save() if it does not exist.


2022-09-30 21:15

I have never used Cake, but
Why don't you check if the user ID exists first and then save it?


2022-09-30 21:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.