我正在使用cakePHP,並且想要向用戶表中添加名和姓列,但是當我通過字段值firstname
& lastname
時,列始終保留爲空,而默認字段填充正常。我可以在CakePHP的用戶表上添加其他字段並仍然使用內置的auth方法嗎?
是否有可能做到這一點,或者我是否需要第二個表來存儲這些值?
更新代碼:
登記表user_controller.php
<?php
$session->flash('register');
echo $form->create('User', array('action' => 'register/'));
echo '<h3>Register</h3>';
echo $form->input('firstname', array('label'=>'First Name'));
echo $form->input('lastname', array('label'=>'Last Name'));
echo $form->input('username');
echo $form->input('email');
echo '<input class="submitimg" type="image" src="' . $basepath . 'img/btn_submit.gif" alt="Submit" />';
echo $form->end();
?>
註冊行動
function register() {
if (!empty($this->data)) {
$password = $this->str_makerand(8,10);
$this->data['User']['password'] = $this->Auth->password($password);
$this->User->create();
$result = $this->User->save($this->data);
if ($result) {
$this->Session->setFlash('Registration complete, an email will be sent with your password', 'default', array(), 'register');
$this->sendNewUserMail($this->data['User']['username'], $this->data['User']['email'], $password);
} else {
$this->Session->setFlash('That username or email address is already taken, please try again', 'default', array(), 'register');
}
}
$this->redirect(array('controller' => 'properties', 'action' => 'index'));
}
MySQL用戶表:
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username char(50) DEFAULT NULL,
firstname varchar(100) NOT NULL,
lastname varchar(100) NOT NULL,
password char(50) DEFAULT NULL,
email varchar(100) NOT NULL,
created datetime NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY email (email),
UNIQUE KEY username (username)
)
感謝您的幫助