views:

431

answers:

4

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

A: 

In cases like these it's considered best practice to have a separate table linked to the user table that includes all the extra information you want to keep.

Konstantinos
+2  A: 

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.

Travis Leleu
A: 

This is my users table:

CREATE TABLE `cake_users` (
    `id` int(11) unsigned NOT NULL auto_increment,
    `username` varchar(100) default NULL,
    `password` varchar(100) default NULL,
    `email` varchar(150) default NULL,
    `firstname` varchar(60) default NULL,
    `lastname` varchar(60) default NULL,
    `priv` int(4) default '0',
    PRIMARY KEY (`id`)
);

I don't have public user registration, but I can create users with the additional fields. This is the code I use in the controller:

function admin_add() {
    if (!empty($this->data)) {
        $this->User->create();
        $this->set('password',$this->Auth->password($this->data['User']['password']));
        if ($this->User->save($this->data)) {
            $this->Session->setFlash(__('The User has been saved', true));
            $this->redirect(array('action'=>'index'));
        } else {
            $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
        }
    }
}
Calvin
A: 

Extra fields are, or course, possible and normal. There is probably a hidden bug somewhere in your code.

Did you try debugging/outputting $this->data after a post? Are your firstname/lastname values showing in the array? Do you have a column whitelist/blacklist in your model which prevents those fields to be saved? How do the input fields look in the output HTML? They are supposed to be named as data[User][firstname] and if they are not, something is terribly wrong..

dr Hannibal Lecter