title: Saving data to 2 tables in the database in CakePHP
body: <p>I have a simple sample project created to test how to add data to 2 tables from one controller in <code>CakePHP</code>, I have a tables called <code>Student</code>, <code>Users</code>, <code>Admin</code> and <code>UserGroups</code>. I need to add a Student to the database table and also add the students username and password to the users table. Here are the SQL for the tables.</p>
  
  <pre><code>CREATE TABLE `students` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_group_id` int(11) DEFAULT NULL,
    `name` varchar(255) DEFAULT NULL,
    `address` varchar(255) DEFAULT NULL,
    `username` varchar(40) DEFAULT NULL,
    `password` varchar(40) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
  
  
  CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_group_id` int(11) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  CREATE TABLE `user_groups` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `role` varchar(255) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
  
  CREATE TABLE `admins` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_group_id` int(11) DEFAULT NULL,
    `name` varchar(255) DEFAULT NULL,
    `post` varchar(255) DEFAULT NULL,
    `username` varchar(40) DEFAULT NULL,
    `password` varchar(40) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p>I created the following function to add the data to the <code>Students</code> table and the <code>Users</code> table. </p>
  
  <pre><code>public function add() {
          if ($this-&gt;request-&gt;is('post')) {
  
                      $userGrp = $this-&gt;request-&gt;data['Student']['user_group_id'];
                      $username = $this-&gt;request-&gt;data['Student']['username'];
                      $pass = $this-&gt;request-&gt;data['Student']['password'];
  
              $this-&gt;Student-&gt;create();
              if ($this-&gt;Student-&gt;save($this-&gt;request-&gt;data)) {
                                  $this-&gt;request-&gt;data['User']['user_group_id'] = $userGrp;
                                  $this-&gt;request-&gt;data['User']['username'] = $username;
                                  $this-&gt;request-&gt;data['User']['password'] = $pass;
                                  if ($this-&gt;Student-&gt;User-&gt;save($this-&gt;request-&gt;data)) {
                                      $this-&gt;Session-&gt;setFlash(__('The student has been saved. Both in to Student and User'));
                                      return $this-&gt;redirect(array('action' =&gt; 'index'));
                                  }
  
              } else {
                  $this-&gt;Session-&gt;setFlash(__('The student could not be saved. Please, try again.'));
              }
          }
          $userGroups = $this-&gt;Student-&gt;UserGroup-&gt;find('list');
          $this-&gt;set(compact('userGroups'));
      }
  </code></pre>
  
  <p>The model for the Student is as follows.</p>
  
  <pre><code>&lt;?php
  App::uses('AppModel', 'Model');
  
  class Student extends AppModel {
  
      public $displayField = 'name';
  
      public $belongsTo = array(
              'UserGroup' =&gt; array(
                      'className' =&gt; 'UserGroup',
                      'foreignKey' =&gt; 'user_group_id',
                      'conditions' =&gt; '',
                      'fields' =&gt; '',
                      'order' =&gt; ''
              )
      );
  
  }
  </code></pre>
  
  <p>When i add the Student details in to the database following error shows. Data is sent to the Student table but not to the Users table</p>
  
  <pre><code>Error: Call to a member function save() on a non-object
  File: D:\wamp\www\MultiTables\app\Controller\StudentsController.php 
  </code></pre>
  
  <p>What am i doing wrong here? please help.</p>