【问题标题】:Symfony ManyToOne relationship getter returns empty objectSymfony ManyToOne 关系 getter 返回空对象
【发布时间】:2013-02-20 11:48:47
【问题描述】:

我将简化我的代码,我有下一个:

医生实体:

    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 实体:

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;
    }
}

好的,问题是,当我执行那段代码时(当然是创建了一个关系并且这个对象存在于数据库中):

\Doctrine\Common\Util\Debug::dump($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 }

如您所见,“医生”对象的所有属性均为空,该对象存在但为空,在我的数据库中该对象存在且不为空。

知道发生了什么吗?

【问题讨论】:

    标签: symfony null doctrine getter many-to-one


    【解决方案1】:

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

    【讨论】:

    • 不知何故与此有关。现在我收到了这个异常:“[1/2] ErrorException: Notice: Undefined index: id in /var/www/vhosts/default/htdocs/Symfony/app/cache/dev/doctrine/orm/Proxies/__CG__KnoidCorcheckBundleEntityDoctor.php 行48 英寸,但前提是我尝试获取医生的身份证。
    • 我假设您已经为 id 定义/生成了一个 getter?
    • 是的,医生实体类中的“public function getId() { return $this->id; }”
    • 我不太确定该怎么做。如果您使用其他设置并在此之后检查对象会发生什么?
    • 你是对的,该项目需要初始化,但由于某种原因它不能与 id 一起使用,但它可以与其他属性(它的名称)一起使用。谢谢你:)
    【解决方案2】:

    Thomas K 的答案在我自己的 Bundle 中对我有用。如果我翻译我所做的:

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

    我加$myPaciente->getDoctor()->getName();

    然后初始化完成,我可以转储 $myPaciente 以及与它相关的医生的所有信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 2017-08-25
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      相关资源
      最近更新 更多