Adrian
Adrian

Reputation: 25

Cakephp Model won't save form input

I have a very simple model User, and it's associated table users with fields 'id', 'username', 'password', 'role', 'created', 'modified'.

The view add.ctp contains:

<div class="users form">
    <?= $this->Form->create('user'); ?>

    <fieldset>
        <legend><?= __('Add user');?></legend>
        <?= $this->Form->input('user.username'); ?>

        <?= $this->Form->input('password'); ?>

        <?= $this->Form->input('role', array('options' => array('admin' => 'Admin', 'customer' => 'Customer'))); ?>

    </fieldset>
    <?= $this->Form->end(__('Submit')); ?>
</div>

The model is set up according to http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html:

class User extends AppModel {
    public $validate = array( 
        'username'  => array(
            'required'  => array(
                'rule'      => array('notEmpty'),
                'message'   => 'You must specify a username'
            )
        ),
        'password'  => array(
            'required'  => array(
                'rule'      => array('notEmpty'),
                'message'   => 'You must specify a password'
            )
        ),
        'role'      => array(
            'valid'     => array(
                'rule'      => array('inList', array('admin','customer')),
                'message'   => 'You must specify a valid role',
                'allowEmpty'=> false
            )
        )
    );
}

And lastly, the controller simply has:

public function add() {
    if($this->request->is('post')) {
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->set('stuff', $this->User->data);
            $this->Session->setFlash(__('The user has been saved.'));
            //return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please try again.'));
        }
    }
}

I've commented out the redirect on success to be able to show the INSERT query on submit (if I allow the redirect, the only query being shown is SELECT's)

So, quite simple, the problem is, when the form is submitted, it generates this query:

INSERT INTO `mytable`.`users` (`modified`, `created`) VALUES ('2014-02-21 13:03:11', '2014-02-21 13:03:11')

How can I determine why it won't insert the submitted fields? And how can I make it?

Upvotes: 0

Views: 607

Answers (1)

Samar Haider
Samar Haider

Reputation: 912

use this <?= $this->Form->create('User'); ?> instead of <?= $this->Form->create('user'); ?>

and use this <?= $this->Form->input('User.username'); ?> instead of <?= $this->Form->input('user.username'); ?>

CakePHP is case sensitive model name must be in CamalCase

Upvotes: 1

Related Questions