I'm having trouble with the code that I'm working with I have two
tables named users and profiles
the user table holds the value for user account creation and logging
in the profiles table hold the additional information of the user.
There are cases that I need to update the secondary field of the user
after creating an account or set their status as active or inactive
but I couldn't make to work the saving of the associated data when I
try to update by calling the function call in the edit profile in my
users_controller I'm not sure if it has something to do with this code
$user =($this->User->save($this->data));
$this->data['Profile']['user_id'] = $this->User->id = $id;
$this->User->Profile->save($this->data);
$this->redirect(array('action'=>'index'),null, true);
I can successfully create an account and retrieve the data but I
couldn't make it work in edit/update mode I'm not sure what I'm
missing...
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users`
(
`id` int(20)unsigned NOT NULL
auto_increment,
`username` varchar(255) NOT NULL,
`password` varchar(40) NOT NULL,
`email` varchar(255) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`confirmed` varchar(1) NOT NULL default '0',
`confirm_code` varchar(36) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `confirm_code` (`confirm_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
In my Profiles table I have these
DROP TABLE IF EXISTS `profiles`;
CREATE TABLE IF NOT EXISTS `profiles`
(
`id` int(20)unsigned NOT NULL auto_increment,
`firstname` varchar(150) NOT NULL,
`lastname` varchar(150) NOT NULL,
`addressone` varchar(250) NOT NULL,
`cityone` varchar(150) NOT NULL,
`stateone` varchar(150) NOT NULL,
`zipone` varchar(50) NOT NULL,
`country` varchar(100) NOT NULL,
`landline` varchar(100) NOT NULL,
`mobile` varchar(100) NOT NULL,
`addresstwo` varchar(250) NOT NULL,
`citytwo` varchar(150) NOT NULL,
`statetwo` varchar(150) NOT NULL,
`ziptwo` varchar(50) NOT NULL,
`landlinetwo` varchar(100) NOT NULL,
`mobiletwo` varchar(100) NOT NULL,
`usertype` varchar(20) NOT NULL default
'Employee',
`user_id` int(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`user_id`) REFERENCES users (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;
User Model
<?php //User Models
class User extends AppModel {
var $name = 'User';
var $hasOne = array(
'Profile' => array(
'className' => 'Profile',
'dependent' => true //This set the associated data in
profiles table to be deleted
),
var $validate = array(
'username' => array(
'notempty' => array(
'rule' => array('minLenght', '8'),
'required' => true,
'allowEmpty' => false,
'message' => 'Email field cannot be empty'
),
'unique' => array(
'rule' => array('checkUnique', 'username'),
'message' => 'User name taken. Use another'
)
),
'password' => array(
'notempty' => array(
'rule' => array('minLenght', 8),
'required' => true,
'allowEmpty' => false,
'message' => 'Password cannot be empty.'
),
'passwordSimilar' => array(
'rule' => 'checkPasswords',
'message' => 'Different password re entered.'
)
)
);
}
?>
Profile Model
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = array (
'User' =>array (
'className' => 'User',
)
);
var $validate = array(
'firstname' => array(
'rule'=> 'notEmpty',
'message' =>'Field must not be empty, please provide your
firstname'
),
'lastname' => array(
'rule'=> 'notEmpty',
'message' =>'Field must not be emptry, please provide your
lastname'
),
'addressone' => array(
'rule'=> 'notEmpty',
'message' =>'Field must not be empty, please provide your
address'
),
'cityone' => array(
'rule'=> 'notEmpty',
'message' =>'Field must not be empty, please provide your
city'
),
'stateone' => array(
'rule'=> 'notEmpty',
'message' =>'Field must not be empty, please provide your
state or province'
),
'country' => array(
'rule'=> 'notEmpty',
'message' =>'Field must not be empty, please provide your
country'
),
'zipone' => array(
'rule'=> 'notEmpty',
'message' =>'Field must not be empty, please provide your zip
code'
),
'landline'=> array(
'rule'=> 'notEmpty',
'message' =>'Field must not be empty, please provide your
landline for contacts'
),
'mobile' => array(
'rule' => 'notEmpty',
'message' =>'Field must not be empty, please provide your
mobile for sending SMS'
),
);
}
?>
Users Controller
<?php
class UsersController extends AppController { #Start AppController
var $name = 'Users';
var $helpers = array ('Html', 'Form');
var $components = array ('Email');
function create_user () {
if (!empty($this->data)) {
if(isset($this->data['User']['password2']))
$this->data['User']['password2hashed'] = $this->Auth-
>password($this->data['User']['password2']);
$this->data['User']['confirm_code'] = String::uuid();
$this->User->create();
$user =($this->User->save($this->data));
if(!empty($user)){
$this->data['Profile']['user_id'] = $this->User->id;
$this->User->Profile->save($this->data);
$this->redirect(array('action'=>'index'),null, true);
}
}
}
function edit_profile($id = null) {
unset($this->User->validate['password'],
$this->User->validate['email'],
$this->User->validate['username']);
$this->User->id = $id;
if (empty($this->data)) {
$this->data = $this->User->read();
if(isset($this->data['User']['password2']))
$this->data['User']['password2hashed'] = $this->Auth-
>password($this->data['User']['password2']);
$this->data['User']['confirm_code'] = String::uuid();
$user =($this->User->save($this->data));
$this->data['Profile']['user_id'] = $this->User->id = $id;
$this->User->Profile->save($this->data);
$this->redirect(array('action'=>'index'),null, true);
}
}
?>
This is my edit_profile view
<h2>User Account Editing</h2>
<?php if ( $session->check('Message.auth')) $session->flash('auth'); ?
>
<?php e ($form->create('User', array('controller'=>'profiles',
'action' =>'edit_profile')));?>
<fieldset>
<?php echo $form->input('id', array('type'=>'hidden')); ?>
<?php echo $form->input ('Profile.firstname', array ('class'=>
'fullwidth', 'label'=> 'Firstname:'));?>
<?php echo $form->input ('Profile.lastname', array ('class'=>
'fullwidth', 'label'=> 'Lastname:'));?>
<?php echo $form->input ('Profile.addressone', array
('class'=> 'fullwidth', 'label'=> 'Address:'));?>
<?php echo $form->input ('Profile.cityone', array ('class'=>
'fullwidth', 'label'=> 'City:')); ?>
<?php echo $form->input ('Profile.stateone', array ('class'=>
'fullwidth', 'label'=> 'State/Province:')); ?>
<?php echo $form->input ('Profile.zipone', array ('class'=>
'fullwidth','label'=> 'Zip Code:')); ?>
<?php echo $form->input ('Profile.country', array ('class'=>
'fullwidth', 'label' => 'Country:')); ?>
<?php echo $form->input ('Profile.landline', array ('class' =>
'fullwidth', 'label' => 'Telephone:')); ?>
<?php echo $form->input ('Profile.mobile', array ('class' =>
'fullwidth', 'label' => 'Mobile')); ?>
<?php echo $form->input('confirmed', array('options' => array
(0,1))); ?>
<?php echo'<p>Set the user account as active <b>1</b> or
inactive <b>0</b></p>'; ?><br/>
<?php echo $form->input ('Profile.usertype', array
('type'=>'hidden', 'value'=>'Customer')); ?>
<?php
echo '<h3>Optional Secondary Field</h3>';
echo ($form->input ('Profile.addresstwo', array
('class'=>'fullwidth', 'label'=>'Second Address:')));
echo ($form->input ('Profile.citytwo', array
('class'=>'fullwidth','label'=>'City:')));
echo ($form->input ('Profile.statetwo', array
('class'=>'fullwidth', 'label'=>'State:')));
echo ($form->input ('Profile.ziptwo', array
('class'=>'fullwidth', 'label'=>'Zip Code:')));
echo ($form->input ('Profile.landlinetwo', array
('class'=>'fullwidth', 'label'=>'Fixedline:')));
echo ($form->input ('Profile.mobiletwo', array
('class'=>'fullwidth', 'label'=>'Mobile:')));
?>
<?php echo ($form->submit('Update', array('div' => false,
'class' => 'submitbutton'))); ?>
</fieldset>
<?php e($form->end()); ?>
Thanks in advance for any help, cheers!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment