Foros del Web » Programando para Internet » PHP » Symfony »

[SOLUCIONADO] Proxies\__CG__\ no puedo acceder al objeto en symfony2

Estas en el tema de Proxies\__CG__\ no puedo acceder al objeto en symfony2 en el foro de Symfony en Foros del Web. Que tal amigos! mi problema consiste que al realizar un repositorio o al utilizar un findBy me retorna objetos pero no puedo acceder a sus ...
  #1 (permalink)  
Antiguo 23/09/2015, 11:46
 
Fecha de Ingreso: septiembre-2013
Mensajes: 61
Antigüedad: 10 años, 7 meses
Puntos: 0
Proxies\__CG__\ no puedo acceder al objeto en symfony2

Que tal amigos! mi problema consiste que al realizar un repositorio o al utilizar un findBy me retorna objetos pero no puedo acceder a sus atributos, haciendo un ladybug_dump($entity); sobre el resultado me aparacere lo siguiente:
Código PHP:
object(Proxies__CG__EmpresaNomnreBundleEntityEntidad
No se en que consiste ese error? Les agradezco su ayuda.

Última edición por carlosml08; 23/09/2015 a las 11:50 Razón: mejorar sintaxis
  #2 (permalink)  
Antiguo 25/09/2015, 07:25
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Proxies\__CG__\ no puedo acceder al objeto en symfony2

Puedes publicar como tienes tu Entity y como estas recuperando los datos ?
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #3 (permalink)  
Antiguo 29/09/2015, 09:24
 
Fecha de Ingreso: septiembre-2013
Mensajes: 61
Antigüedad: 10 años, 7 meses
Puntos: 0
Respuesta: Proxies\__CG__\ no puedo acceder al objeto en symfony2

Entity:
Código PHP:
Ver original
  1. <?php
  2.  
  3. namespace ABC\IsystemBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6.  
  7. /**
  8.  * AbcRelationship
  9.  *
  10.  * @ORM\Table(name="abc_relationship")
  11.  * @ORM\Entity
  12.  */
  13. class AbcRelationship
  14. {
  15.     /**
  16.      * @var integer
  17.      *
  18.      * @ORM\Column(name="id", type="integer", nullable=false)
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue(strategy="IDENTITY")
  21.      */
  22.     private $id;
  23.  
  24.     /**
  25.      * @var string
  26.      *
  27.      * @ORM\Column(name="relationship", type="string", length=80, nullable=false)
  28.      */
  29.     private $relationship;
  30.  
  31.     /**
  32.      * @var \DateTime
  33.      *
  34.      * @ORM\Column(name="created_date", type="datetime", nullable=false)
  35.      */
  36.     private $createdDate;
  37.  
  38.     /**
  39.      * @var string
  40.      *
  41.      * @ORM\Column(name="relationship_en", type="string", length=80, nullable=false)
  42.      */
  43.     private $relationshipEn;
  44.  
  45.     /**
  46.      * Get id
  47.      *
  48.      * @return integer
  49.      */
  50.     public function getId()
  51.     {
  52.         return $this->id;
  53.     }
  54.  
  55.     /**
  56.      * Set relationship
  57.      *
  58.      * @param string $relationship
  59.      * @return AbcRelationship
  60.      */
  61.     public function setRelationship($relationship)
  62.     {
  63.         $this->relationship = $relationship;
  64.    
  65.         return $this;
  66.     }
  67.  
  68.     /**
  69.      * Get relationship
  70.      *
  71.      * @return string
  72.      */
  73.     public function getRelationship()
  74.     {
  75.         return $this->relationship;
  76.     }
  77.  
  78.     /**
  79.      * Set createdDate
  80.      *
  81.      * @param \DateTime $createdDate
  82.      * @return AbcRelationship
  83.      */
  84.     public function setCreatedDate($createdDate)
  85.     {
  86.         $this->createdDate = $createdDate;
  87.    
  88.         return $this;
  89.     }
  90.  
  91.     /**
  92.      * Get createdDate
  93.      *
  94.      * @return \DateTime
  95.      */
  96.     public function getCreatedDate()
  97.     {
  98.         return $this->createdDate;
  99.     }
  100.    
  101.     /**
  102.      * Set relationshipEn
  103.      *
  104.      * @param string $relationshipEn
  105.      * @return AbcRelationship
  106.      */
  107.     public function setRelationshipEn($relationshipEn)
  108.     {
  109.         $this->relationshipEn = $relationshipEn;
  110.    
  111.         return $this;
  112.     }
  113.  
  114.     /**
  115.      * Get relationshipEn
  116.      *
  117.      * @return string
  118.      */
  119.     public function getRelationshipEn()
  120.     {
  121.         return $this->relationshipEn;
  122.     }
  123. }
Consulto el objeto de la siguiente manera:
Código PHP:
Ver original
  1. $relationship = $this->emMy->getRepository('ABCIsystemBundle:AbcRelationship')->findOneBy(array('relationshipEn'=>'Mother'));
Al realizar un
Código PHP:
Ver original
  1. ladybug_dump($relationship);
le aparece lo siguiente:
Código PHP:
Ver original
  1. object(Proxies__CG__ABC\IsystemBundle\Entity\AbcRelationship)
  2.  
  3. lazyPropertiesDefaults array (0)
  4. __cloner__ object (Closure)
  5. __initializer__ object (Closure)
  6. __isInitialized__ bool true
Cuando en realidad deberia poder acceder a los atributos que tiene el objeto.

Última edición por carlosml08; 29/09/2015 a las 09:36
  #4 (permalink)  
Antiguo 29/09/2015, 11:16
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Proxies\__CG__\ no puedo acceder al objeto en symfony2

Eso no es un error, es el comportamiento esperado, Doctrine te regresa un Proxy.
Para acceder a los datos solo tienes que invocar el método que requieras del objeto que te regresa el repositorio
Código PHP:
Ver original
  1. $relationship = $this->emMy->getRepository('ABCIsystemBundle:AbcRelationship')->findOneBy(array('relationshipEn'=>'Mother'));
  2. $relationship->getCreatedDate();
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #5 (permalink)  
Antiguo 29/09/2015, 11:36
 
Fecha de Ingreso: septiembre-2013
Mensajes: 61
Antigüedad: 10 años, 7 meses
Puntos: 0
Respuesta: Proxies\__CG__\ no puedo acceder al objeto en symfony2

Efectivamente puedo acceder a los atributos del objeto, pero lo que necesito es el objeto de la entidad para poder settearlo a otro objeto de entidad y poder persistir la otra entidad, asi:
Código PHP:
Ver original
  1. $membersContacts->getRelationship($relationship);
  2. $this->emMy->persist($membersContacts);
  3. $this->emMy->flush();
Y como me retorna un Proxy no lo interpreta como objeto y no persiste ningun valor en ese atributo. Tambien probé accediendo a su Id y no funciona porque no es un objeto
Código PHP:
Ver original
  1. $membersContacts->getRelationship($relationship->getId());
  #6 (permalink)  
Antiguo 29/09/2015, 12:00
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Proxies\__CG__\ no puedo acceder al objeto en symfony2

Siguiendo tu ejemplo tienes dos detalles.
1.- utiliza set para establecer la relación con el otro objeto
Código PHP:
Ver original
  1. $membersContacts->setRelationship($relationship);
  2.     $this->emMy->persist($membersContacts);
  3.     $this->emMy->flush();
2.- en tu caso no funciona por que la propiedad que declaraste es un string. Tienes que indicar que esa propiedad es una relación hacia otra Entidad.
http://docs.doctrine-project.org/pro...n-mapping.html
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #7 (permalink)  
Antiguo 29/09/2015, 13:57
 
Fecha de Ingreso: septiembre-2013
Mensajes: 61
Antigüedad: 10 años, 7 meses
Puntos: 0
Respuesta: Proxies\__CG__\ no puedo acceder al objeto en symfony2

Asi como lo dices lo he hecho, creo que debí ser mas explicito:
Código PHP:
$relationship $this->emMy->getRepository('ABCIsystemBundle:AbcRelationship')->findOneBy(array('relationshipEn'=>'Mother'));
$membersContacts = new AbcMembersContacts();
$membersContacts->setMember($student->getMember());
$membersContacts->setContact($parent->getMember());
$membersContacts->getRelationship($relationship);
$membersContacts->setMainContact('f');
$membersContacts->setContactOrder(2);
$this->emMy->persist($membersContacts);
$this->emMy->flush(); 
Entidad AbcMembersContacts:
Código PHP:
<?php

namespace ABCIsystemBundleEntity
;

use 
DoctrineORMMapping as ORM;

/**
 * AbcMembersContacts
 *
 * @ORM\Table(name="abc_members_contacts")
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Entity(repositoryClass="ABC\IsystemBundle\Repository\AbcMembersContactsRepository")
 */
class AbcMembersContacts
{
     
/**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    
private $id;
    
    
/**
     * @var string
     *
     * @ORM\Column(name="main_contact", type="string", nullable=false)
     */
    
private $mainContact;

    
/**
     * @var \AbcMembers
     *
     * @ORM\ManyToOne(targetEntity="AbcMembers", inversedBy="contacts", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="member", referencedColumnName="id")
     * })
     */
    
private $member;

    
/**
     * @var \AbcMembers
     *
     * @ORM\ManyToOne(targetEntity="AbcMembers", inversedBy="contactsm", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="contact", referencedColumnName="id")
     * })
     */
    
private $contact;

    
/**
     * @var \AbcRelationship
     *
     * @ORM\ManyToOne(targetEntity="AbcRelationship", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="relationship", referencedColumnName="id")
     * })
     */
    
private $relationship;

    
/**
     * @var \DateTime
     *
     * @ORM\Column(name="created_date", type="datetime", nullable=false)
     */
    
private $createdDate;
        
    
/**
     * @var integer
     * @ORM\Column(name="contact_order", type="integer", nullable=true)
     */
    
private $contactOrder;
    
    
/**
    * @ORM\OneToOne(targetEntity="\ABC\PreEnrollmentBundle\Entity\EnrollPayers", mappedBy="memberContact", cascade={"persist", "remove"})
    */
    
protected $payer;
    
    public function 
setPayer(ABCPreEnrollmentBundleEntityEnrollPayers $payer null)
    {
        
$this->payer $payer;
        
$payer->setMemberContact($this);
        return 
$this;
    }
    
    public function 
getPayer(){
        return 
$this->payer;
    }
    
    
/**
     * Get id
     *
     * @return integer 
     */
    
public function getId()
    {
        return 
$this->id;
    }

    
/**
     * Set mainContact
     *
     * @param string $mainContact
     * @return AbcMembersContacts
     */
    
public function setMainContact($mainContact)
    {
        
$this->mainContact $mainContact;
    
        return 
$this;
    }

    
/**
     * Get mainContact
     *
     * @return string 
     */
    
public function getMainContact()
    {
        return 
$this->mainContact;
    }

    
/**
     * Set member
     *
     * @param \ABC\IsystemBundle\Entity\AbcMembers $member
     * @return AbcMembersContacts
     */
    
public function setMember(ABCIsystemBundleEntityAbcMembers $member)
    {
        
$this->member $member;
    
        return 
$this;
    }

    
/**
     * Get member
     *
     * @return \ABC\IsystemBundle\Entity\AbcMembers 
     */
    
public function getMember()
    {
        return 
$this->member;
    }

    
/**
     * Set contact
     *
     * @param \ABC\IsystemBundle\Entity\AbcMembers $contact
     * @return AbcMembersContacts
     */
    
public function setContact(ABCIsystemBundleEntityAbcMembers $contact)
    {
        
$this->contact $contact;
    
        return 
$this;
    }

    
/**
     * Get contact
     *
     * @return \ABC\IsystemBundle\Entity\AbcMembers 
     */
    
public function getContact()
    {
        return 
$this->contact;
    }

    
/**
     * Set relationship
     *
     * @param \ABC\IsystemBundle\Entity\AbcRelationship $relationship
     * @return AbcMembersContacts
     */
    
public function setRelationship(ABCIsystemBundleEntityAbcRelationship $relationship null)
    {
        
$this->relationship $relationship;
    
        return 
$this;
    }

    
/**
     * Get relationship
     *
     * @return \ABC\IsystemBundle\Entity\AbcRelationship 
     */
    
public function getRelationship()
    {
        return 
$this->relationship;
    }

    
/**
    * @ORM\PrePersist
    */
    
public function setCreatedDateValue()
    {
        
$this->createdDate = new DateTime();
    }


    
/**
     * Set createdDate
     *
     * @param \DateTime $createdDate
     * @return AbcMembers
     */
    
public function setCreatedDate($createdDate)
    {
        
$this->createdDate $createdDate;
    
        return 
$this;
    }

    
/**
     * Get createdDate
     *
     * @return \DateTime 
     */
    
public function getCreatedDate()
    {
        return 
$this->createdDate;
    }
    
    
/**
     * Set academicOrder
     *
     * @param integer $order
     * @return AbcMembersContacts
     */
    
public function setContactOrder($order)
    {
        
$this->contactOrder $order;
    
        return 
$this;
    }

    
/**
     * Get order
     *
     * @return integer 
     */
    
public function getContactOrder()
    {
        return 
$this->contactOrder;
    }
    
}
  #8 (permalink)  
Antiguo 29/09/2015, 14:11
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Proxies\__CG__\ no puedo acceder al objeto en symfony2

Muy bien, pero sigues cometiendo uno de los errores; mira esto:
Código PHP:
Ver original
  1. $membersContacts->getRelationship($relationship);
Si no tas el error ? getRelationship no puede establecer la relación algo que es obvio si miras tu código:
Código PHP:
Ver original
  1. public function getRelationship()
  2.     {
  3.         return $this->relationship;
  4.     }
Si notas que ese método no tiene parámetros ?
Utiliza el setRelationship($relationship)
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #9 (permalink)  
Antiguo 29/09/2015, 14:20
 
Fecha de Ingreso: septiembre-2013
Mensajes: 61
Antigüedad: 10 años, 7 meses
Puntos: 0
Respuesta: Proxies\__CG__\ no puedo acceder al objeto en symfony2

Tienes razon! sinceramente estaba ciego. Muchas gracias por la ayuda y paciencia.

Etiquetas: symfony2
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:42.