Reputation: 2269
When I post to this controller, I get this back as the response: Fatal error: Call to a member function find() on a non-object in /app/Controller/AppController.php on line 26
which probably has to do with using $this->data()
explicitly. I was using CakePHP save without form
per a recommendation in there, but since I'm not using a form to send the data (thus not using $this->request->data()
), I'd like to know what the replacement is for $this->data()
so I can get this working.
My database table is is submissions_favorites
.
This is my SubmissionFavorite model:
class SubmissionFavorite extends AppModel {
var $name = 'SubmissionFavorite';
var $belongsTo = array(
'User' => array(
'className' => 'User'
)
);
}
This is AjaxController (what I'm posting to):
class AjaxController extends AppController {
var $layout = 'ajax'; // uses an empty layout
var $autoRender = false; // renders nothing by default
var $uses = 'SubmissionFavorite';
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->loginRedirect = array('controller' => 'home', 'action' => 'index');
$this->Auth->allow('addFavorite');
$this->Auth->flashElement = 'flash_error';
}
public function addFavorite() {
$this->autoRender = false;
$this->data['SubmissionFavorite']['user_id'] = $this->Session->read('Auth.User.id');
$this->data['SubmissionFavorite']['submission_id'] = $_POST['id'];
$this->data['SubmissionFavorite']['when'] = DboSource::expression('NOW()');
$message = array('success' => 'success');
$toReturn = json_encode($message);
if ($this->RequestHandler->isAjax()) {
if ($this->Session->read('Auth.User')) {
$this->SubmissionFavorite->save($this->data);
return $toReturn;
} else {
$login = array('login' => 'Please log in to add favorites');
return json_encode($login);
}
}
}
Line 26 in my AppController is:
protected function getSubmissionCount() {
$totalSubmissions = $this->Submission->find('count');
return $totalSubmissions;
}
Which is totally unrelated to anything else. I didn't even add anything to AppController when I wrote the new method within my AjaxController, so I'm not sure how it's relevant (or why I'm even getting an error in that file).
Upvotes: 1
Views: 2436
Reputation: 840
First, following cake conventions, you should name your model SubmissionsFavorite (note the s between Submission and Favorite). This is because your table name is composed by 2 words, even representing a relationship between 2 other tables.
Also, you can't do $this->Submission->... on AppController without telling cake whatever is "Submission". Take a look at this link to see how to initialize Submission model and use it on AppController: Can I use one model inside of a different model in CakePHP?
Regards.
Upvotes: 1
Reputation: 15350
Try to change all
var $name = 'SubmissionFavorite';
to:
public $name = 'SubmissionFavorite';
Also change: var $uses = 'SubmissionFavorite';
to: public $uses = array ('SubmissionFavorite');
Upvotes: 1