if Ususario
hasMany EmpresasA
then you have to do
$this->Usuario->EmpresasA->create()
etc...
also you don't have to loadModel Usuario
and EmpresasA
because cake load them automatically when the correct relationships are set
Вопрос
I want to make an insertion at the same time.
Models:
Empresa:
<?php
App::uses('AppModel', 'Model');
class Empresa extends AppModel {
var $name = 'Empresa';
public $belongsTo = array(
'UsuariosA' => array(
'className' => 'Usuario',
'foreignKey' => 'usuario_id'));
public $hasMany = array(
'IntegrantesA' => array(
'className' => 'Integrante'),
'PlanificacionesA' => array(
'className' => 'Planificacion'),
'MetodologiaEmpresasA' => array(
'className' => 'MetodologiaEmpresa')
);
}
Usuario:
<?php
App::uses('AppModel', 'Model');
class Usuario extends AppModel {
var $name = 'Usuario';
public $hasMany = array(
'ForosA' => array(
'className' => 'Foro'),
'DetalleSesiones' => array(
'className' => 'Detallesesion'),
'UsuariorolesA' => array(
'className' => 'Usuariorol'),
'TablalogsA' => array(
'className' => 'Tablalog'),
'DocumentosA' => array(
'className' => 'Documento'),
'DocentesA' => array(
'className' => 'Docente'),
'EmpresasA' => array(
'className' => 'Empresa')
);
}
View/Empresas/add.ctp
echo $this->Form->create('Usuario', array('type' => 'file', 'action' => 'add'));
echo $this->Form->input('Usuario.id', (array('label' => 'Nombre la empresa', 'type' => 'text', 'default' => 'lal')));
echo $this->Form->input('Usuario.password', array('type' => 'password', 'label' => 'Password', 'default' => 'lal'));
echo $this->Form->input('Empresa.0.id');
echo $this->Form->input('Empresa.0.nombre_largo', (array('label' => 'Nombre Largo', 'type' => 'text', 'default' => 'lal')));
echo $this->Form->input('Empresa.0.nombre_corto', (array('label' => 'Nombre Corto', 'type' => 'text', 'default' => 'lal')));
echo $this->Form->input('Empresa.0.correo', array('type' => 'email', 'default' => 'lal@lal'));
echo $this->Form->input('Empresa.0.docente', array('type' => 'tel', 'default' => '1'));
echo $this->Form->input('Empresa.0.semestre', array('type' => 'tel', 'default' => '1'));
echo $this->Form->input('Empresa.o.gestion', array('type' => 'tel', 'default' => '2014'));
echo $this->Form->end('Registrar');
echo $this->Form->end('Cancelar');
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?
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.
UsuariosController
class UsuariosController extends AppController {
public $helpers = array('Html', 'Form');
var $uses = array('Empresa', 'Usuario');
public function add(){
if (!empty($this->request->data)) {
$this->loadModel('Usuario');
$this->Usuario->create();
$usuario = $this->Usuario->save($this->request->data);
if (!empty($usuario)) {
$this->loadModel('Empresa');
$this->Empresa->create();
$this->request->data['Empresa']['usuario_id'] = $this->Usuario->id;
$this->Usuario->Empresa->save($this->request->data);
}
}
}
}
I can insert in Usuario with no problem, but this error appears when I try to insert in usuario and empresa.
Error: Call to a member function save() on a non-object
File: C:\xampp\htdocs\app\Controller\UsuariosController.php
Line: 35
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.
unset($this->Usuario->Empresa->validate['usuario_id']);
$usuario = $this->Usuario->saveAssociated($this->request->data);
Thanks in advance.
Решение
if Ususario
hasMany EmpresasA
then you have to do
$this->Usuario->EmpresasA->create()
etc...
also you don't have to loadModel Usuario
and EmpresasA
because cake load them automatically when the correct relationships are set
Другие советы
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?
this is because in the code you put echo $this->Form->create('Usuario', array('type' => 'file', 'action' => 'add'));
but if you want to call an action in Empresas, you should put echo $this->Form->create('Empresa', array('type' => 'file', 'action' => 'add'));
Usuario and Empresa are supposed to have a one-one relationship. would it be okay this?:
Yeah it is ok to do this
try the following code and if
class UsuariosController extends AppController {
public $helpers = array('Html', 'Form');
var $uses = array('Empresa', 'Usuario');
public function add(){
if (!empty($this->request->data)) {
$this->Usuario->create();
$this->Usuario->saveAssociated($this->request->data);
}
}
If there is anything, tell me.
Hope it helps!