lastInsertId() public method

Returns the ID generated from the previous INSERT operation.
public lastInsertId ( mixed $source = null ) : mixed
$source mixed The source to get an id for.
return mixed
示例#1
0
 /**
  * Setup the admin user.
  *
  * @return bool
  */
 public function setupAdmin()
 {
     $answer = strtoupper($this->in('<question>Would you like to [c]reate a new user, or use an [e]xisting user?</question>', array('C', 'E')));
     $userMap = Configure::read('Forum.userMap');
     $statusMap = Configure::read('Forum.statusMap');
     // New User
     if ($answer === 'C') {
         $this->install['username'] = $this->_newUser('username');
         $this->install['password'] = $this->_newUser('password');
         $this->install['email'] = $this->_newUser('email');
         $result = $this->db->execute(sprintf("INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`) VALUES (%s, %s, %s, %s);", $this->install['table'], $userMap['username'], $userMap['password'], $userMap['email'], $userMap['status'], $this->db->value(Sanitize::clean($this->install['username'])), $this->db->value(Security::hash($this->install['password'], null, true)), $this->db->value($this->install['email']), $this->db->value($statusMap['active'])));
         if ($result) {
             $this->install['user_id'] = $this->db->lastInsertId();
         } else {
             $this->out('<error>An error has occurred while creating the user</error>');
             return $this->setupAdmin();
         }
         // Old User
     } else {
         if ($answer === 'E') {
             $this->install['user_id'] = $this->_oldUser();
             // Redo
         } else {
             return $this->setupAdmin();
         }
     }
     // Give ACL
     $result = ClassRegistry::init('Forum.Access')->add(array('parent_id' => $this->install['acl_admin'], 'foreign_key' => $this->install['user_id']));
     if (!$result) {
         $this->out('<error>An error occurred while granting administrator access</error>');
         return $this->setupAdmin();
     }
     return true;
 }