title: CakePHP - Getting NULL from a session variable when trying to add to a Model's options array
body: <p>I'm using cakephp 2.3.0. I searched in the manual for quite awhile and searched the internet, but I haven't found the answer. Here is my high level scenario:</p>
  
  <p>** Basically, I'm just trying to add a variable and a value to the options array, so that when cakephp builds the SQL insert into statement, it has this required column and value. **</p>
  
  <ul>
  <li>The user logs in to a web page with username and password</li>
  <li>Within the controller, I use the model to query my user table to get the id # of the user</li>
  <li>I put the ID # into a session variable</li>
  <li>Next, the user is logged in and then fills out a web form and submits</li>
  <li>In my controller, I attempt to add to the model's options array with the value of that id stored in the session variable (this is where my trouble lies)....see details below</li>
  </ul>
  
  <p>In my UsersController, I have this code snippet:</p>
  
  <pre><code>public function login() {
          if ($this-&gt;request-&gt;is('post')) {
              if ($this-&gt;Auth-&gt;login()) {                    
                  $condition = array("User.username" =&gt; $this-&gt;request-&gt;data['User']['username']);
                  $this-&gt;Session-&gt;write('userId', $this-&gt;User-&gt;find('first', 
                              array (
                              'conditions' =&gt; array ($condition),
                              'fields' =&gt; array ('User.id'))));                   
                  $this-&gt;redirect($this-&gt;Auth-&gt;redirect());                   
              } else {
                  $this-&gt;Session-&gt;setFlash(__('Invalid username or password, try again'));
              }
          }
      }
  </code></pre>
  
  <p>In my ActivitiesController, I have this:</p>
  
  <pre><code>class ActivitiesController extends AppController {
  
      var $components = array('Session');
  
      public function createActivity() {
          if ($this-&gt;request-&gt;is('post')) {
                $this-&gt;Activity-&gt;create();
               $user = $this-&gt;Session-&gt;read('userId');
              //debug($user);
              debug($user['User']['id']);
              $ownerID = ($user['User']['id']);
              $this-&gt;Activity-&gt;set(array('owner_id' =&gt; $ownerID));
              if ($this-&gt;Activity-&gt;save($this-&gt;request-&gt;data)) {
                  $this-&gt;Session-&gt;setFlash(__('The user has been saved'));
                  $this-&gt;redirect(array('action' =&gt; 'home'));
              } else {
                  $this-&gt;Session-&gt;setFlash(__('The user could not be saved. Please, try again.'));
              }
          }
  </code></pre>
  
  <p>My debug will print a value of 5, which is what I would expect to see. But, what I get is a database error, which outputs on the webpage the SQL INSERT INTO code and it tells me that my owner_id cannot be NULL. Which is correct, since that is how I setup this up in the DB table.</p>
  
  <p>So, I'm getting a NULL when trying to put the value of the ID from the session variable into my options array. I would still consider myself more of a beginner to cakephp. Any help would be appreciated. Thanks.</p>