after working with Cakephp for a while, I never had before an issue like this, I am struggling for a few hours and I cannot understand what is happening here. I will try to explain as better as I can:
I am working with many bespoke plugins (projects), and all of them use the same controller/action for adding users (add method), but one of them saves the user in the database and returns false (the save method) without any kind of errors. I am using the Debug plugin to check the insert SQL queries, but there are only select queries, not insert. The weird thing is that the validation returns always this ^
"{"Message":"User account creation failure","data":{"email":{"_isUnique":"This email has already been used."}}}"
` But the user is saved correctly, I really do not understand, it seems like I am saving the user twice, but the save method is invoqued only once. Any clue?
Note: does not matter what email I try to register, I always check first that the email does not exist in the database, but the save method returns most of the time false. 99% of the time fails, but on very few occasions it worked (save does not return false).
If I add dd('whatever') after invoking the save method, the condition is not matched most of the time (because it returns false, but the user is added to the db).
if ($this->Users->save($user)) {
dd('whatever'); // Never executed
}
UPDATE:
After the "method save" returns FALSE, I checked if the user exists in the database (by code):
if (Configure::read('App.theme') === 'NameProject') {
$exists = $this->Users
->find()
->select(['id'])
->where(['email' => $user->email])
->first();
dd($xists); // NULL
if ($exists) {
$this->Flash->success(__d($this->theme, 'Your account has been created));
return $this->redirect('/home');
}
}
The method $this->Users->find() cannot find the user, but if I check in the database the user is there, that's very very weird.
UPDATE: After working many hours on this issue, I have realized that the only difference between the projects (plugins) is the add.ctp templates and the databases (as I commented above). The project that fails uses the method control instead the deprecated input ($this->Form->control), but I cannot find any other differences.