Forked from PoeticIntensity/Error created even when field is full.
Last active
August 29, 2015 14:04
-
-
Save commercial-hippie/c4f46c7cdc82fb24632f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function add() { | |
// You don't need to call this, | |
// if you need the Users model you | |
// can use $this->Admins->Users | |
// $this->loadModel('Users'); | |
$admin = $this->Admins->newEntity($this->request->data); | |
// This is not required! | |
// $user = $this->Users->newEntity($this->request->data); | |
if ($this->request->is('post')) { | |
// Note I changed it to ->data() and not ->data[] | |
$admin->user->type = $this->request->data("is_super") ? 'super' : 'admin'; | |
// Validate and save the user. | |
// By default all first level relationships | |
// will be saved and validated. | |
if ($this->Users->save($admin)) { | |
$this->Flash->success('The credentials have been saved.'); | |
return $this->redirect(['action' => 'index']); | |
} else { | |
$this->Flash->error('The admin was not saved.'); | |
} | |
} | |
$users = $this->Admins->Users->find('list'); | |
$this->set(compact('admin', 'users')); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//WE NEED TO VALIDATE BOTH THE NEW USER AND THE NEW ADMIN BEFORE WE SAVE ANYTHING. (BY DEFAULT, IT WILL ONLY VALIDATE THE USER BEFORE SAVING THE USER) | |
// ------------------------ | |
// Validation gets called on save, no need to call it yourself. | |
// ------------------------ | |
if($this->Users->validate($user)) { | |
if($this->Admins->validate($admin)) { | |
// ------------------------ | |
// You don't need to save the two users | |
// seperately, Cake does this for you! | |
// ------------------------ | |
if($this->Users->save($user)) { | |
$this->Flash->success('The credentials have been saved.'); | |
//UPDATE THE user_id CORRECTLY FOR THE NEW ADMIN. | |
$admin->user_id = $user->id; | |
if ($this->Admins->save($admin)) { | |
$this->Flash->success('The admin has been saved.'); | |
return $this->redirect(['action' => 'index']); | |
// ------------------------ | |
// No need for the exit :) | |
// ------------------------ | |
exit; | |
} else { | |
//NONE OF THIS FAIL CODE SHOULD EVER BE RUN, DUE TO THE PREVIOUS VALIDATION, BUT JUST IN CASE, I'M LEAVING IT HERE. | |
//GOTTA GET RID OF THE USER THAT WAS JUST SAVED NOW. | |
$this->Users->delete($user); | |
$this->Flash->error('The admin was not saved.'); | |
} | |
} else { | |
$this->Flash->error('The credentials were not saved.'); | |
} | |
} else { | |
$this->Flash->error('Please check your information and try again.'); | |
} | |
} else { | |
$errors = $user->errors(); | |
$this->Flash->error($errors["username"]["unique"]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment