You are here: cakephp » cakephp not saving in different model
cakephp - not saving in different model
- Written By
- Shadow
- Submitted At
- 2010-02-24 11:44:52
- Num Views
- 883
- Category
- CakePHP
|
I have an issue with my models and functions not playing nice, and was wondering if you could help. This is the code: public function register() { if (!empty($this->data)) { if ($this->User->save($this->data)) { $this->Session->setFlash('Your registration information was accepted'); $this->Session->write('user', $this->data['User']['username']); $this->redirect(''); } else { $this->Session->setFlash('There was a problem saving this information'); } } } the error I get is: Fatal error: Call to a member function save() on a non-object in /home/deono/cake/unwanted/app/controllers/frontpage_controller.php on line 10 WHYYY? By Shadow @ 2010-02-24 11:44:52
|
|
Hehe, [quote]models and functions not playing nice[/quote]. I like that. The code is in fact correct. It's the controller you are using that is causing the problem. You are in a controller called FrontpageController from what I can see. What cake does is automatically loads the database Model for the Frontpage Controller which gives you access to call functions like $this->Frontpage->save() What you are doing is calling $this->User->save() from within the controller called Frontpage. To access the User Model from within another controller you have to let CakePHP know that you want to use that model here as it is not the automatically loaded in the Frontpage Controller as only the Frontpage Model is loaded automatically. To load a new model in CakePHP you just have to say $this->loadModel('User'); What this does is it gives you access to $this->User and all the User Model's functions like $this->User->save(). If you had the register function within the User Controller then this would not have been a problem, but because it is in Frontpage Controller (different name) you need to load the User model. public function register() { if (!empty($this->data)) { $this->loadModel('User'); if ($this->User->save($this->data)) { $this->Session->setFlash('Your registration information was accepted'); $this->Session->write('user', $this->data['User']['username']); $this->redirect(''); } else { $this->Session->setFlash('There was a problem saving this information'); } } } Alternatively you can just move the register function into the User Controller. This will cause CakePHP to load the User model automatically and you don't have to use $this->loadModel('User') as CakePHP does that automatically. This will obviously change your url from /frontpage/register/ to /user/register/. Hope this helps :) By PHPin24 @ 2010-02-24 11:57:01
|
|
YES!! It works now, I used your first suggestion and loaded the User model with $this->loadModel('User');, the problem was that I had it in the user_controller, but I have my login validation there, so I had the problem of the user not being able to register before they log in.. Haha.. Funny, I know.. By Shadow @ 2010-02-24 12:03:19
|
