你好我與Symfony2的工作,我有這些關係:Symfony2的關係學說
表醫:
/**
* @ORM\OneToMany(targetEntity="Cita", mappedBy="medico")
*/
protected $citas;
表citas:
/**
* @ORM\ManyToOne(targetEntity="Medico", inversedBy="citas")
*/
protected $medico;
然後,我有這樣的代碼,其預約被創建爲一名醫生。
$em = $this->get('doctrine')->getEntityManager();
$cita = new Cita();
$empleado = $this->get('security.context')->getToken()->getUser();
$cita->setEmpleado($empleado);
$cliente = $this->getDoctrine()->getRepository('miomioBundle:Usuario')->find($this->getRequest()->query->get('cliente'));
$cita->setFechacita(new \DateTime($this->getRequest()->query->get('fecha')));
$cita->setCliente($cliente);//asociacion de cita a usuario.
$cita->setFechaoper(new \DateTime('now'));
$medico = $this->getDoctrine()->getRepository('miomioBundle:Medico')->find($this->getRequest()->query->get('medico'));
$cita->setMedico($medico);
$em->persist($cita);
$em->persist($cliente);
$em->persist($empleado);
$em->flush();
,然後試圖得到醫生的約會:
public function pintarAction($id)
{
$return_arr = array();
$medico = $this->getDoctrine()->getRepository('miomioBundle:Medico')->find($id);
$citas = $medico->getCitas();
foreach($citas as $cita){
return new Response('entra');
$array['id'] = (string)$cita->getId();
$array['title'] = $cita->getCliente()->getNombre() . ' '. $cita->getCliente()->getApellido1();
$array['empleado'] = $cita->getEmpleado()->getUsername();
if ($cita->getInforme()){
$array['informe'] = $cita->getInforme()->getId();
$array['className'] = 'informe';
}
else{
$array['informe'] = 'NO';
$array['className'] = '';
}
$array['fecha'] = $cita->getFechaoper()->format('d-m-Y H:i:s');
$array['start'] = $cita->getFechacita()->format('Y-m-d H:i:s');
$array['allDay'] = false;
array_push($return_arr,$array);
}
return new Response(json_encode($return_arr), 200, array('Content-Type', 'text/json'));
}
其中變量$法醫學是確定的。但變量$ citas是空的。爲什麼?
有人知道我做錯了嗎?問候和感謝。
1.您的'pintarAction'方法將在您輸入'foreach'循環時立即退出,這是由於'return'子句; 2.檢查你的db是否有選定的medico的真實數據; 3.不要生氣,使用[FOSRestBundle](https://github.com/FriendsOfSymfony/FRestBundle)! – moonwave99