6

コードを簡単に説明します。次は次のとおりです。

医師の実体:

    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;

       ...

    }

患者エンティティ:

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 }

ご覧のとおり、「doctor」オブジェクトのすべての属性はnullであり、オブジェクトは存在しますが空です。私のDBには、このオブジェクトが存在し、空ではありません。

何が起こっているのかについて何か考えはありますか?

4

2 に答える 2

3

これは、プロキシオブジェクトがまだ初期化されていないためです。それを初期化する1つの方法は、オブジェクトをクエリすること$doctor->getId()です。その後オブジェクトをダンプすると、すべての属性が「表示」されていることがわかります。

于 2013-02-20T11:53:13.010 に答える
1

トーマスKの答えは私自身のバンドルで私のために働いた。私がしたことを翻訳すると:

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

私は追加します$myPaciente->getDoctor()->getName();

次に、初期化が行われ、それに関連する医師に関するすべての情報を含む$myPacienteをダンプできました。

于 2014-08-21T09:08:09.030 に答える