Wednesday, March 7, 2012

Re: linked models and save

UP : Thans to theanswers above and more research, I found the
solution, so I share it for those who could be interested.

First, I removed validations in User model. It blocked the save method
in the Tutor Controller.

Second, here is the code :

if ($this->request->is('post'))
{

$dataForm=$this->request->data('Tutor');

$dataUser = array('User' => array('username' =>
$dataForm['LOGINTUTOR'], 'password' =>
AuthComponent::password($dataForm['password']), 'role' => 'tutor'));

$this->Tutor->User->save($dataUser);

$dataTutor = array('Tutor' => array('user_id' => $this->Tutor-
>User->id, 'FIRSTNAMETUTOR' =>
$dataForm['FIRSTNAMETUTOR'],'LASTNAMETUTOR' =>
$dataForm['LASTNAMETUTOR']));

if ($this->Tutor->save($dataTutor)) {

$this->Session->setFlash('The student has been
saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add the tutor.');
}
}

On Mar 4, 8:49 pm, Cédric Chabert <ced.chab...@gmail.com> wrote:
> The user is not saved. The tutor is saved but not the user.
>
> On Mar 4, 6:38 pm, Tilen Majerle <tilen.maje...@gmail.com> wrote:
>
>
>
>
>
>
>
> > now, user is saved ?
>
> > if it's still not, then is problem FOR user in validation, after call
> > save() method for User, debug($this->Tutor->User->validationErrors);
>
> > i'm sure you failed with $this->request->data('PASSWORDTUTOR')
> > --
> > Lep pozdrav, Tilen Majerlehttp://majerle.eu
>
> > 2012/3/4 Cédric Chabert <ced.chab...@gmail.com>
>
> > > Thank you for this answer !
>
> > > Actually there is validated beforeSave, and the password is hashed.
>
> > > class User extends AppModel {
> > >    public $name = 'User';
>
> > >    public $hasOne = 'Tutor';
>
> > >    public $validate = array(
> > >        'username' => array(
> > >            'required' => array(
> > >                'rule' => array('notEmpty'),
> > >                'message' => 'A username is required'
> > >            )
> > >        ),
> > >        'password' => array(
> > >            'required' => array(
> > >                'rule' => array('notEmpty'),
> > >                'message' => 'A password is required'
> > >            )
> > >        ),
> > >        'role' => array(
> > >            'valid' => array(
> > >                'rule' => array('inList', array('student','tutor')),
> > >                'message' => 'Please enter a valid role',
> > >                'allowEmpty' => false
> > >            )
> > >        )
> > >    );
>
> > > public function beforeSave() {
> > >    if (isset($this->data[$this->alias]['password'])) {
> > >        $this->data[$this->alias]['password'] =
> > > AuthComponent::password($this->data[$this->alias]['password']);
> > >    }
> > >    return true;
> > > }
> > > }
>
> > > On Mar 4, 6:01 pm, Tilen Majerle <tilen.maje...@gmail.com> wrote:
> > > > $dataUser should be something like this
>
> > > > $dataUser = array('User' => array('username' => $this->request-
>
> > > > >data('LOGINTUTOR'), 'password' => $this->request-
> > > > >data('PASSWORDTUTOR'), 'role' => 'tutor'));
>
> > > > and make sure that model is not validated before save, maybe here is
> > > > problem too :)
> > > > --
> > > > Lep pozdrav, Tilen Majerlehttp://majerle.eu
>
> > > > 2012/3/4 Cédric Chabert <ced.chab...@gmail.com>
>
> > > > > $dataUser = array('username' => $this->request-
> > > > > >data('LOGINTUTOR'), 'password' => $this->request-
> > > > > >data('PASSWORDTUTOR'), 'role' => 'tutor');
>
> > > --
> > > Our newest site for the community: CakePHP Video Tutorials
> > >http://tv.cakephp.org
> > > Check out the new CakePHP Questions sitehttp://ask.cakephp.organdhelp
> > > others with their CakePHP related questions.
>
> > > To unsubscribe from this group, send email to
> > > cake-php+unsubscribe@googlegroups.com For more options, visit this group
> > > athttp://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


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

No comments: