Symfony ManyToOne relationship getter returns empty object
Asked Answered
P

2

6

I'll simplifly my code, I have te next:

Doctor entity:

    use ...\...\Entity\Paciente;

    class Doctor extends Usuario {

        public function __construct() {
            ...
            $this->pacientes = new ArrayCollection();
            ...

        }


        /**
         * Número de colegiado - numColegiado
         * 
         * @var string
         *
         * @ORM\Column(name="numColegiado", type="string", length=255, unique=true)
         */
        protected $numColegiado;


        /**
         * @ORM\OneToMany(targetEntity="Paciente", mappedBy="doctor")
         * @var \Doctrine\Common\Collections\ArrayCollection
         */
        private $pacientes;

       ...

    }

Paciente entity:

use \...\...\Entity\Doctor;

...

class Paciente extends Usuario {

    }

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @ORM\ManyToOne(targetEntity="Doctor", inversedBy="pacientes")
     * @ORM\JoinColumn(name="doctorNum", referencedColumnName="numColegiado", nullable=TRUE)
     * 
     * @var type 
     */
    protected $doctor;

    ...

    /**
     * Set doctor
     *
     * @param Doctor $doctor
     * @return Paciente
     */
    public function setDoctor(Doctor $doctor = null)
    {
        $this->doctor = $doctor;

        return $this;
    }

    /**
     * Get doctor
     *
     * @return Doctor 
     */
    public function getDoctor()
    {
        return $this->doctor;
    }
}

Ok, the matter is, when I execute that code (of course there is a relationship created and this object exists in the database):

\Doctrine\Common\Util\Debug::dump($paciente->getDoctor());

It prints that follows:

object(stdClass)#804 (28) { ["__CLASS__"]=> string(34) "Knoid\CorcheckBundle\Entity\Doctor" ["__IS_PROXY__"]=> bool(true) ["__PROXY_INITIALIZED__"]=> bool(false) ["id"]=> NULL ["numColegiado"]=> NULL ["pacientes"]=> NULL ["nombre"]=> NULL ["apellidos"]=> NULL ["dni"]=> NULL ["tipo"]=> NULL ["username"]=> NULL ["usernameCanonical"]=> NULL ["email"]=> NULL ["emailCanonical"]=> NULL ["enabled"]=> NULL ["salt"]=> NULL ["password"]=> NULL ["plainPassword"]=> NULL ["lastLogin"]=> NULL ["confirmationToken"]=> NULL ["passwordRequestedAt"]=> NULL ["groups"]=> NULL ["locked"]=> NULL ["expired"]=> NULL ["expiresAt"]=> NULL ["roles"]=> NULL ["credentialsExpired"]=> NULL ["credentialsExpireAt"]=> NULL }

As you can see, all the atributes of the "doctor" object are null, the object exists but it's empty, in my DB this object exists and it isn't empty.

Any idea of what's happening ?

Puncture answered 20/2, 2013 at 11:48 Comment(0)
D
3

This is because the proxy object is not initialised yet. One way to initialise it, is by querying the object e.g. $doctor->getId(). If you dump the object after that, you'll see that all the attributes are 'visible'

Doctrinaire answered 20/2, 2013 at 11:53 Comment(5)
Somehow it's relatet with that. Now I recived this exception: "[1/2] ErrorException: Notice: Undefined index: id in /var/www/vhosts/default/htdocs/Symfony/app/cache/dev/doctrine/orm/Proxies/__CG__KnoidCorcheckBundleEntityDoctor.php line 48" but only if I try to get the doctor's id.Godiva
I assume you have defined/generated a getter for the id?Doctrinaire
Yes, "public function getId() { return $this->id; }" in the doctor entity classGodiva
I'm not really sure what to make of that. What happens if you use another setting and check the object after that?Doctrinaire
You were right, the item needs to be initialized, but for some reason it didn't work with the id, but with other attribute (name for it case) it did. Thank you :)Godiva
U
1

The answer of Thomas K worked for me in my own Bundle. If I translate what I did :

$myPaciente = $em->getRepository('MyBundle:Paciente')->findOneBy(array('numColegiado' => $value));

I add $myPaciente->getDoctor()->getName();

Then the initialisation was done and I could dump $myPaciente with all the information about the doctor related to it.

Unsuccessful answered 21/8, 2014 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.