Wednesday, February 4, 2009

Re: Please help on how to edit/update data in hasOne relationship

@Brian

Yes I have check the data validation, in fact I can create a new user
successfully inserting the required data without problem If I use the
create_user function call supplying and removing the required data to
check the validation.

If I change the code instead of updating the profile field it inserts
another profile rather than updating the already save profile with the
same user_id as the first profile but is link to the user that I tried
to update so in effect in my view it will show 2 users with exactly
the same profile link together if I delete one of them it will
effectively delete the user and the associated profiles.

So if it inserts a new profile I presume the validation is working...,
Is my idea correct?

Thanks for your help.


On Feb 4, 11:45 pm, brian <bally.z...@gmail.com> wrote:
> Are you sure your models validate when you try to save on edit? Change
> your code to test if they validate before saving. That might lead you
> to the problem.
>
> On Wed, Feb 4, 2009 at 4:46 AM, xeroxss <xero...@gmail.com> wrote:
>
> > Hello to all,
>
> > 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' =>
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
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: