Symfony ManyToOne 关系 getter 返回空对象

标签 symfony null doctrine getter many-to-one

我将简化我的代码,下一步是:

医生实体:

    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 }

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

知道发生了什么吗?

最佳答案

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

关于Symfony ManyToOne 关系 getter 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14979123/

相关文章:

php - Symfony Doctrine findBy 然后映射

jquery - Symfony - 使用 Doctrine 创建复杂查询

php - Symfony2 理解 Doctrine 查询加入

excel - VBA 如何从函数返回 null

sql - 为什么在 PostgreSQL 查询中对 DESC 进行排序时 NULL 值排在第一位?

PHP: Doctrine: order joined records

mysql - 如何在单个字段中存储多个选择值 Symfony2

Symfony2 表单集合无法加载类型 CollectionType

scala - 我可以为 == 重现 Scala 的行为吗?

php - 我是否正确地执行了 Doctrine 子类?为什么会出现错误?