-1

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?

Community
  • 1
  • 1
BIOS
  • 1,655
  • 5
  • 20
  • 36
  • declare `var $name = 'Users';` into your model and then try – GBD Nov 27 '12 at 16:14
  • Added to the Users model and no change unfortunately. – BIOS Nov 27 '12 at 16:18
  • I meant just best practice. Users works I often by mistake make class's plural – Tim Lieberman Nov 27 '12 at 16:20
  • Actually you are spot on! I changed the model class to user and updated the code in the controller to reference 'User' and it now writes to the DB! Great catch :) – BIOS Nov 27 '12 at 16:21
  • Yep. I actually moved away from cake real quick because of their coding standards. You should use Symfony2 if your going to spend time learning a framework. – Tim Lieberman Nov 27 '12 at 16:26
  • Really? To be honest I have found the learning curve for cake rather painful so far. It's like pulling teeth trying to figure out even basic stuff as the documentation is so poor. I chose cake because we have alot of existing apps that use it and I said I'd try weather the initial storm but for sure my first impressions of it haven't be great. – BIOS Nov 27 '12 at 16:38
  • I would defiantly check out symfony2. http://symfony.com/ It's most watched on github and is setting the new industry standard. – Tim Lieberman Nov 27 '12 at 17:50

1 Answers1

2

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.

Tim Lieberman
  • 571
  • 2
  • 5
  • 23