title: Posting to a controller with ajax in CakePHP 2.0
body: <p>When I post to this controller, I get this back as the response: <code>Fatal error: Call to a member function find() on a non-object in /app/Controller/AppController.php on line 26</code> which probably has to do with using <code>$this-&gt;data()</code> explicitly. I was using <a href="https://stackoverflow.com/questions/2642015/cakephp-save-without-form">CakePHP save without form</a>
  per a recommendation in there, but since I'm not using a form to send the data (thus not using <code>$this-&gt;request-&gt;data()</code>), I'd like to know what the replacement is for <code>$this-&gt;data()</code> so I can get this working.</p>
  
  <p>My database table is is <code>submissions_favorites</code>.</p>
  
  <p>This is my SubmissionFavorite model:</p>
  
  <pre><code>class SubmissionFavorite extends AppModel {
  
      var $name = 'SubmissionFavorite';
  
      var $belongsTo = array(
          'User' =&gt; array(
              'className' =&gt; 'User'
          )
      );
  
  }
  </code></pre>
  
  <p>This is AjaxController (what I'm posting to):</p>
  
  <pre><code>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-&gt;Auth-&gt;loginRedirect = array('controller' =&gt; 'home', 'action' =&gt; 'index');
          $this-&gt;Auth-&gt;allow('addFavorite');
          $this-&gt;Auth-&gt;flashElement = 'flash_error';
      }
  
  public function addFavorite() {
      $this-&gt;autoRender = false;
  
      $this-&gt;data['SubmissionFavorite']['user_id'] = $this-&gt;Session-&gt;read('Auth.User.id');
      $this-&gt;data['SubmissionFavorite']['submission_id'] = $_POST['id'];
      $this-&gt;data['SubmissionFavorite']['when'] = DboSource::expression('NOW()');
  
      $message = array('success' =&gt; 'success');
  
      $toReturn = json_encode($message);
  
      if ($this-&gt;RequestHandler-&gt;isAjax()) {
          if ($this-&gt;Session-&gt;read('Auth.User')) {
              $this-&gt;SubmissionFavorite-&gt;save($this-&gt;data);
              return $toReturn;
          } else {
              $login = array('login' =&gt; 'Please log in to add favorites');
              return json_encode($login);
          }
      }
  }
  </code></pre>
  
  <p>Line 26 in my AppController is:</p>
  
  <pre><code>protected function getSubmissionCount() {
      $totalSubmissions = $this-&gt;Submission-&gt;find('count');
      return $totalSubmissions;
  }
  </code></pre>
  
  <p>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).</p>