Jose Diaz-Gonzalez home

How CakePHP Automagic fails me (or How I failed at the FormHelper)

Okay, so I have a Model::find() that looks like the following:

<?php
    function __findEditProfile(){
        return $this->find('first', array(
            'conditions' => array("{$this->alias}.{$this->primaryKey}" => User::get('id')),
            'contain' => false,
            'fields' => array(
                'id', 'first_name', 'last_name', 'email', 
                'job_title', 'phone_number', 'photo_file_name')));
    }
?>

And I have an action, UsersController::profile(), with the following structure:

<?php
    function profile() {
        if (!empty($this->data)) {
            $this->data['User']['id'] = User::get('id');
            if ($this->User->save($this->data, true, array(
                    'id', 'first_name', 'last_name', 'email', 'job_title', 'phone_number',
                    'password', 'new_password', 'new_password_confirm'))) {
                $this->Session->setFlash(__('Your profile has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->data = $this->User->resetDataForRegistration($this->data);
                $this->Session->setFlash(__('Your profile could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->find('edit_profile');
        }
    }
?>

and has the following form:

<div>
    <?php $session->flash(); ?>
    <?php echo $form->create('User', array(
                    'url' => array('controller' => 'users', 'action' => 'profile'))); ?>
        <fieldset>
            <legend><?php echo __('Edit Profile', true); ?></legend>
            <?php
                echo $form->input('User.first_name', array(
                    'div' => 'input text required',
                    'label' => __('First Name', true),
                    'type' => 'text'));
                echo $form->input('User.last_name', array(
                    'div' => 'input text required',
                    'label' => __('Last Name', true),
                    'type' => 'text'));
                echo $form->input('User.email', array(
                    'div' => 'input text required',
                    'label' => __('Email Address', true),
                    'type' => 'text'));
                echo $form->input('User.job_title', array(
                    'div' => 'input text required',
                    'label' => __('Job Title', true),
                    'type' => 'text'));
                echo $form->input('User.phone_number', array(
                    'div' => 'input text required',
                    'label' => __('Phone Number', true),
                    'type' => 'text'));
                echo $form->input('User.photo', array(
                    'div' => 'input file',
                    'label' => __('Personal Photo', true),
                    'type' => 'file'));
            ?>
        </fieldset>
    <?php echo $form->submit(); ?>
    <?php echo $form->end(); ?>
</div>

I also have the following route setup

<?php
    Router::connect('/profile', array('controller' => 'users', 'action' => 'profile'));
?>

Tell me why CakePHP thought it was a good idea to have the form submit to /users/profile/1 ?

Because I have configured the FormHelper to submit to default to the User model, and $this->data['User']['id'] is set! Either modify the find to remove the id, or modify the form to not submit with the User as default. Below are the examples in the order I proposed them:

<?php
    function __findEditProfile(){
        return $this->find('first', array(
            'conditions' => array("{$this->alias}.{$this->primaryKey}" => User::get('id')),
            'contain' => false,
            'fields' => array(
                'first_name', 'last_name', 'email', 
                'job_title', 'phone_number', 'photo_file_name')));
    }
?>
<?php echo $form->create('', array(
                'url' => array('controller' => 'users', 'action' => 'profile'))); ?>

I used the first example to get the job done, but you can swat the flies or find cows that eat trees in Brooklyn for all I care ;)


blog comments powered by Disqus