title: cakephp: Call to a member function save() on a non-object and two table insertion
body: <p>I want to make an insertion at the same time.</p>
  
  <p>Models:</p>
  
  <p>Empresa:</p>
  
  <pre><code> &lt;?php
   App::uses('AppModel', 'Model');
  
   class Empresa extends AppModel {
  var $name = 'Empresa';
  
  public $belongsTo = array(
      'UsuariosA' =&gt; array(
          'className' =&gt; 'Usuario',
          'foreignKey' =&gt; 'usuario_id'));
  
  public $hasMany = array(
  'IntegrantesA' =&gt; array(
      'className' =&gt; 'Integrante'),
  'PlanificacionesA' =&gt; array(
      'className' =&gt; 'Planificacion'),
  'MetodologiaEmpresasA' =&gt; array(
      'className' =&gt; 'MetodologiaEmpresa')
      );
   }
  </code></pre>
  
  <p>Usuario:</p>
  
  <pre><code>  &lt;?php
    App::uses('AppModel', 'Model');
    class Usuario extends AppModel {
  var $name = 'Usuario';
  
  public $hasMany = array(
  'ForosA' =&gt; array(
      'className' =&gt; 'Foro'),
  'DetalleSesiones' =&gt; array(
      'className' =&gt; 'Detallesesion'),
  'UsuariorolesA' =&gt; array(
      'className' =&gt; 'Usuariorol'),
  'TablalogsA' =&gt; array(
      'className' =&gt; 'Tablalog'),
  'DocumentosA' =&gt; array(
      'className' =&gt; 'Documento'),
  'DocentesA' =&gt; array(
      'className' =&gt; 'Docente'),
  'EmpresasA' =&gt; array(
      'className' =&gt; 'Empresa')
      );
      }
  </code></pre>
  
  <p>View/Empresas/add.ctp</p>
  
  <pre><code>echo $this-&gt;Form-&gt;create('Usuario', array('type' =&gt; 'file', 'action' =&gt; 'add'));
  echo $this-&gt;Form-&gt;input('Usuario.id', (array('label' =&gt; 'Nombre la empresa', 'type' =&gt; 'text', 'default' =&gt; 'lal')));
  echo $this-&gt;Form-&gt;input('Usuario.password',  array('type' =&gt; 'password', 'label' =&gt; 'Password', 'default' =&gt; 'lal'));
  
  echo $this-&gt;Form-&gt;input('Empresa.0.id');
  echo $this-&gt;Form-&gt;input('Empresa.0.nombre_largo', (array('label' =&gt; 'Nombre Largo', 'type' =&gt; 'text', 'default' =&gt; 'lal')));
  echo $this-&gt;Form-&gt;input('Empresa.0.nombre_corto', (array('label' =&gt; 'Nombre Corto', 'type' =&gt; 'text', 'default' =&gt; 'lal')));
  echo $this-&gt;Form-&gt;input('Empresa.0.correo', array('type' =&gt; 'email', 'default' =&gt; 'lal@lal'));
  echo $this-&gt;Form-&gt;input('Empresa.0.docente', array('type' =&gt; 'tel', 'default' =&gt; '1'));
  echo $this-&gt;Form-&gt;input('Empresa.0.semestre', array('type' =&gt; 'tel', 'default' =&gt; '1'));
  echo $this-&gt;Form-&gt;input('Empresa.o.gestion', array('type' =&gt; 'tel', 'default' =&gt; '2014'));
  echo $this-&gt;Form-&gt;end('Registrar');
  echo $this-&gt;Form-&gt;end('Cancelar');
  </code></pre>
  
  <p>One thing that kind of bothers me is that even if this view is in Empresas, it calls an action of Usuarios. Is that ok?</p>
  
  <p>Then, Usuario and Empresa are supposed to have a one-one relationship. would it be okay this?:
  Usuario Model: public $hasOne = 'Empresa';
  Empresa Model: public $belonsTo = 'Usuario';
  I couldn't make it work so that's why it has a one-many relationship. However as it is now I want to insert in Usuario and Empresa at the same time, of course I would have to insert in Usuario first.</p>
  
  <p>UsuariosController</p>
  
  <pre><code>class UsuariosController extends AppController {
      public $helpers = array('Html', 'Form');
      var $uses = array('Empresa', 'Usuario');
  
      public function add(){
  
      if (!empty($this-&gt;request-&gt;data)) {
          $this-&gt;loadModel('Usuario');
          $this-&gt;Usuario-&gt;create();           
  
          $usuario = $this-&gt;Usuario-&gt;save($this-&gt;request-&gt;data);
  
          if (!empty($usuario)) {
          $this-&gt;loadModel('Empresa');
          $this-&gt;Empresa-&gt;create();
  
                   $this-&gt;request-&gt;data['Empresa']['usuario_id'] = $this-&gt;Usuario-&gt;id;
  
          $this-&gt;Usuario-&gt;Empresa-&gt;save($this-&gt;request-&gt;data);
                    }
             }
        }
  }
  </code></pre>
  
  <p>I can insert in Usuario with no problem, but this error appears when I try to insert in usuario and empresa.</p>
  
  <pre><code>Error: Call to a member function save() on a non-object 
  File: C:\xampp\htdocs\app\Controller\UsuariosController.php 
  Line: 35
  </code></pre>
  
  <p>Also, I wanted to use this lines, so even if the usuario insertion would be ok but not the empresa insertion, then it wouldn't insert anything at all. But I'm confused as where would be the right place for them.</p>
  
  <pre><code>unset($this-&gt;Usuario-&gt;Empresa-&gt;validate['usuario_id']);
  $usuario = $this-&gt;Usuario-&gt;saveAssociated($this-&gt;request-&gt;data);
  </code></pre>
  
  <p>Thanks in advance.</p>