Search code examples
cakephpcakephp-modelphp

Cake PHP registration form


Possible Duplicate:
Call to a member function on a non-object

I have just started learning cake php and am attempting to create a very simple registration form consisting of one field. I'm having trouble interacting with my model however. Code below:

This is my Register Controller:

class RegisterController extends AppController{

    public $helpers = array('Form', 'Html', 'Js');

    public function index(){

    }

}

This is my register controllers index.ctp view:

echo $this->Form->create('Users', array('controller' => 'Users', 'action' => 'createUser'));
echo $this->Form->input('username');
echo $this->Form->end('Register');

I then created a UsersController.php file and a Users.php (model) file to handle the validation and saving of the form data.

UsersController.php:

class UsersController extends AppController{


    public function createUser(){

        $this->Users->create();

        $this->Users->save($this->Request->data);

    }

}

And the Users.php model file:

class Users extends AppModel {

    public $validation = array(

        'username' => array(

            'rule1' => array(
                'rule' => 'isUnique',
                'message' => 'That username is already taken'
                ),
            'rule2' => array(

                'rule' => 'nonEmpty',
                'message' => 'This field cannot be empty',
                'required' => true

            )
        )
    );

}

Whenever I hit the 'register' button on my form however, I get the following error:

Call to a member function create() on a non-object

It's referring to the following line in my UsersController.php file:

$this->Users->create();

I have a users table created in my cake database that the php files can see it. I don't understand why my UsersController.php file can't write to its model?


Solution

  • You have to follow the coding standards with CakePHP. In this case the class "Users" will not work. You have to use class "User" and refactor the code to look for User.