title: Save data in two tables in CakePHP
body: <p>I have a simple form in Cake : </p>
  
  <p><img src="https://i.stack.imgur.com/hVhYu.png" alt="enter image description here"></p>
  
  <p>I want to save the informations in the users and companies tables .</p>
  
  <p>Users Table : </p>
  
  <p><img src="https://i.stack.imgur.com/sFht0.png" alt="enter image description here"></p>
  
  <p>Companies table :</p>
  
  <p><img src="https://i.stack.imgur.com/46m81.png" alt="enter image description here"></p>
  
  <p>I'm in need to get two sql queries .</p>
  
  <p>First of all : 
  - Insert the new users with a new company id which will be created in companies table .</p>
  
  <p>For instance with this case, testza, </p>
  
  <p>users : (new entry)
  testza password company id (new if of the new company entry)</p>
  
  <p>companies (new entry) :
  Id (3) Creaz (name) 500 000 (revenue_generated) .</p>
  
  <p>I guess i'm clear :</p>
  
  <p>companies/signup (view)</p>
  
  <pre><code>&lt;div class="page-header"&gt;
  
      &lt;h1&gt;Company Registration&lt;/h1&gt;
  
  &lt;/div&gt;
  
  &lt;?php echo $this-&gt;Form-&gt;create('Company'); ?&gt;
  
          &lt;?php echo $this-&gt;Form-&gt;input('User.username',array('label'=&gt;'Login')); ?&gt;
  
          &lt;?php echo $this-&gt;Form-&gt;input('User.password',array('label'=&gt;'Password')); ?&gt;
  
          &lt;?php echo $this-&gt;Form-&gt;input('User.passwordconfirm',array('label'=&gt;'Confirm password','type'=&gt;'password')); ?&gt;
  
          &lt;hr&gt;
  
          &lt;?php echo $this-&gt;Form-&gt;input('Company.company_id', 
          array(
              'label'=&gt;'Company',
              'options' =&gt; $companies,
              'empty' =&gt; 'Select Company Name'
          ));
          ?&gt;
  
          &lt;?php echo $this-&gt;Form-&gt;input('Company.revenue_generated',array('label'=&gt;'Revenue Generated')); ?&gt;
  
  &lt;?php echo $this-&gt;Form-&gt;end('Envoyer'); ?&gt;
  </code></pre>
  
  <p>the action signup of the Controller CompaniesController</p>
  
  <pre><code>&lt;?php
  
  class CompaniesController extends AppController{
  
      public $uses = array('User');
  
      public function signup() {
  
          $this-&gt;loadModel('Company');
  
          $companies = $this-&gt;Company-&gt;find('list'); //we get the authors from the database
  
          $this-&gt;set('companies', $companies);
  
          if($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put') ){
  
              $d = $this-&gt;request-&gt;data['User'];
              $c = $this-&gt;request-&gt;data['Company'];
  
              if($d['password'] != $d['passwordconfirm']){
  
                  $this-&gt;Session-&gt;setFlash("Les mots de passes ne correspondent pas","notif",array('type'=&gt;'error'));
  
              }
  
              else{
  
                  if(empty($d['password']))
  
                      unset($d['password']);
  
                  if($this-&gt;User-&gt;save($d) || $this-&gt;Company-&gt;save($c)){
  
                      $this-&gt;Session-&gt;setFlash("L'utilisateur a bien été enregistré","notif");
  
                  }
  
              }
  
          }
      }
  ..............
  </code></pre>
  
  <p>User Model :</p>
  
  <pre><code>&lt;?php 
  
  class User extends AppModel{
  
      public $actsAs = array('Containable'); 
  
       public $belongsTo = array(        
          'Town' =&gt; array(
              'className' =&gt; 'Town'
          ),
          'Company' =&gt; array(
              'className' =&gt; 'Company'
          )
      ); 
  
      public $recursive = -1; 
  
      public $order = 'User.id DESC';
  
      public $validate = array(
  
          'username' =&gt; array(
  
              'rule'       =&gt; 'isUnique',
  
              'allowEmpty' =&gt; false,
  
              'message'    =&gt; "Ce nom d'utilisateur est déja pris"
  
          ),
  
          'password' =&gt; array(
  
              'rule'       =&gt; 'notEmpty',
  
              'message'    =&gt; "Vous ne pouvez pas entrer de mot de pase"
  
          )
  
      );
  
      public function beforeSave($options = array()){
  
          if(!empty($this-&gt;data['User']['password'])){
  
              $this-&gt;data['User']['password'] = AuthComponent::password($this-&gt;data['User']['password']);
  
          }
  
          return true; 
  
      }
  
  }
  </code></pre>
  
  <p>Company Model :</p>
  
  <pre><code>&lt;?php 
  
  class Company extends AppModel{
  
      public $validate = array(
          'revenue_generated' =&gt; array(
              'rule' =&gt;'notEmpty',
              'message' =&gt; 'Enter Your Generated Revenue.',
              'required' =&gt; true
          )
      );
  
  }
  </code></pre>