Ver Mensaje Individual
  #11 (permalink)  
Antiguo 13/10/2012, 16:19
Avatar de portalmana
portalmana
 
Fecha de Ingreso: septiembre-2007
Ubicación: Montevideo-Uruguay
Mensajes: 633
Antigüedad: 16 años, 6 meses
Puntos: 80
Respuesta: Relaciones con Doctrine 2

Código PHP:
Ver original
  1. <?php
  2. namespace modulos\acceso\models;
  3.  
  4. /**
  5.  * @Entity
  6.  * @Table(name="usuarios")
  7.  **/
  8. class Usuario
  9. {
  10.     /**
  11.      * @Id
  12.      * @Column(type="string", length=30)
  13.      **/
  14.     protected $usuario;
  15.    
  16.     /**
  17.      * @Column(type="string", length=210)
  18.      **/
  19.     protected $correo;
  20.    
  21.     /**
  22.      * @Column(type="string", length=50, name="nombre_usuario")
  23.      **/
  24.     protected $nombre;
  25.    
  26.     /**
  27.      * @Column(type="string", length=50)
  28.      **/
  29.     protected $clave;
  30.    
  31.     /**
  32.      * @ManyToOne(targetEntity="Perfil", inversedBy="usuarios", cascade={"persist"}, fetch="EAGER")
  33.      * @JoinColumn(name="id_perfil", referencedColumnName="id_perfil")
  34.      */
  35.     protected $perfil;
  36.    
  37.     public function __construct()
  38.     {
  39.        $this->perfil = new Perfil();
  40.     }
  41.    
  42.     /** GETERS */
  43.     public function getUsuario()    { return $this->usuario; }
  44.     public function getCorreo()     { return $this->correo; }
  45.     public function getNombre()     { return $this->nombre; }
  46.     public function getClave()      { return $this->clave; }
  47.     public function getPerfil()     { return $this->perfil; }
  48.    
  49.     /** SETERS */
  50.     public function setUsuario($usuario)    { $this->usuario    = $usuario; }
  51.     public function setCorreo($correo)      { $this->correo     = $correo; }
  52.     public function setNombre($nombre)      { $this->nombre     = $nombre; }
  53.     public function setClave($clave)        { $this->clave      = $clave; }
  54.    
  55.     public function setPerfil(Perfil $perfil)
  56.     {
  57.         $perfil         ->addUsuario($this);
  58.         $this->perfil   = $perfil;
  59.     }
  60.  
  61. }
Código PHP:
Ver original
  1. <?php
  2. namespace modulos\acceso\models;
  3.  
  4. /**
  5.  * @Entity
  6.  * @Table(name="perfiles")
  7.  **/
  8. class Perfil
  9. {
  10.     /**
  11.      * @Id
  12.      * @GeneratedValue (strategy="AUTO")
  13.      * @Column(type="integer", name="id_perfil")
  14.      **/
  15.     protected $id;
  16.    
  17.     /** @Column(type="string", length=50) **/
  18.     protected $perfil;
  19.    
  20.     /**
  21.      * @OneToMany(targetEntity="Usuario", mappedBy="perfil", cascade={"persist"})
  22.      */
  23.     protected $usuarios;
  24.  
  25.     public function __construct()
  26.     {
  27.         $this->usuarios = new \Doctrine\Common\Collections\ArrayCollection();
  28.     }
  29.    
  30.     /**
  31.      * Agrego un Usuario al Perfil.
  32.      * @param \modulos\acceso\models\Usuario $usuario
  33.      */
  34.     public function addUsuario(Usuario $usuario)
  35.     {
  36.         $this->usuarios[] = $usuario;
  37.     }
  38.    
  39.    
  40.     /** GETERS */
  41.     public function getId()         { return $this->id; }
  42.     public function getPerfil()     { return $this->perfil; }
  43.     public function getUsuario()    { return $this->usuario; }
  44.    
  45.     /** SETERS */
  46.     public function setId($id)              { $this->id         = $id; }
  47.     public function setPerfil($perfil)      { $this->perfil     = $perfil; }
  48.     public function setUsuarios($usuario)   { $this->usuario    = $usuario; }  
  49.    
  50.     /** Funciones Especiales */
  51.     public function toArray()
  52.     {
  53.         return get_object_vars($this);
  54.     }
  55.  
  56. }

Estas son las entidades
Estas es una de las formas que intente para guardar.
Generar un objeto Perfil agregarle el Usuario y luego persistirlo
Código PHP:
Ver original
  1. // Traigo un Usuario
  2. $usuario =  $this->_ormManager->find('\modulos\acceso\models\Usuario',$id);
  3.                $usuario->setCorreo($valores['correo']);
  4.                $usuario->setNombre($valores['nombre']);
  5.                $usuario->setClave($valores['clave']);
  6.                $usuario->setPerfil($perfiles->getPerfilById($valores['idPerfil']));
  7.                
  8.                $perfil = $this->_ormManager->find('\modulos\acceso\models\Perfil',$id);
  9.                $perfil->addUsuario($usuario);
  10.                $this->_ormManager->persist($perfil);
  11.               $this->_ormManager->flush();

Esta es la mas rebuscada.
La otra es ir solo contra Usuario

Gracias
__________________
"La imaginación es más importante que el conocimiento. El conocimiento es limitado, mientras que la imaginación no" -- A.Einstein
objetivophp.com,twitter.com/objetivophp