Ad

Can I Have Additional Fields On The Users Table In CakePHP & Still Use The Built-in Auth Methods?

I'm using cakePHP and I want to add a First and Last name column to the Users table, but when I pass through the field values firstname & lastname the columns are always left null, while the default fields are populate fine.

Is it possible to do this or do I need to have a second table to store these values?

Update code:

Registration form

        <?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();
    ?>

Register action in user_controller.php

    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 users table:

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)
)

Thanks for your help

Ad

Answer

You can definitely have additional fields on your users table. The Auth component will just authorize against your username and password fields in that table. In fact, if you read up on the Auth component's attributes you'll find that you can set which fields Cake will use as the user/pass fields.

Sounds like your problem is a bug. Please post some code and database schema and I'll take a look to help you further.

Ad
source: stackoverflow.com
Ad