Symfony ManyToOne关系getter返回空对象 [英] Symfony ManyToOne relationship getter returns empty object

查看:137
本文介绍了Symfony ManyToOne关系getter返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



医生实体:

 使用... \ ... \Entity\Paciente; 

class Doctor extends Usuario {

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

}


/ **
*Númerode 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\Doctor; 

...

class Paciente扩展Usuario {

}

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


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

...

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

return $ this;
}

/ **
*获取医生
*
* @return Doctor
* /
public function getDoctor )
{
return $ this->医生;
}
}

好的,事情是,当我执行该代码(当然有一个关系创建,这个对象存在于数据库中):

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

它打印如下:

  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} 

如您所见,doctor对象的所有属性都为null,对象存在但它是空的,在我的数据库中,这个对象存在,它不是空的。



任何想法发生什么?

解决方案

这是因为代理对象尚未初始化。初始化它的一种方法是通过查询对象,例如 $ doctor->的getId()。如果你之后转储对象,你会看到所有的属性都是'可见的'


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 ?

解决方案

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'

这篇关于Symfony ManyToOne关系getter返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆