我将简化我的代码,接下来我有:
医生单位:
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 }
正如你所看到的,“医生”对象的所有属性都是空的,对象存在但它是空的,在我的数据库中这个对象存在并且它不是空的。
知道发生了什么吗?