A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearCan I have additional fields on the users table in CakePHP & still use the built-in auth methods? - page 1
User InfoPosts
Can I have additional fields on the users table in CakePHP & still use the built-in auth methods?#1
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

posted date: 2009-04-13 07:53:00


Re: Can I have additional fields on the users table in CakePHP & still use the built-in auth methods?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-13 07:53:01


Re: Can I have additional fields on the users table in CakePHP & still use the built-in auth methods?#3
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.

posted date: 2009-04-13 08:00:00


Re: Can I have additional fields on the users table in CakePHP & still use the built-in auth methods?#4
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)); } }}

posted date: 2009-04-13 08:50:00


Re: Can I have additional fields on the users table in CakePHP & still use the built-in auth methods?#5
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..

posted date: 2009-04-13 10:59:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.