doujiu1447 2014-01-17 21:37
浏览 302
已采纳

EntityManager getRepository,方法find(id)返回控制器

My problem is that when I try to find a database record using $em->find method, it returns me a Controller.

Let me put an example:

Neostat\DiagnosticoBundle\Controller\ComponentController.php:

$em = $this->getDoctrine()->getEntityManager();
$diagnostico = $em->getRepository('NeostatDiagnosticoBundle:Diagnostico')->find($id);
var_dump(get_class($diagnostico));

It returns Neostat\DiagnosticoBundle\Controller\ComponentController.

But I have an entity called Diagnostico.php in src/Neostat/DiagnosticoBundle/Entity/Diagnostico.php:

namespace Neostat\DiagnosticoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Neostat\PacienteBundle\Entity\Paciente;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Diagnostico
 *
 * @ORM\Table(name="diagnostico")
 * @ORM\Entity(repositoryClass="Neostat\DiagnosticoBundle\Entity\DiagnosticoRepository")
 * @UniqueEntity(fields={"nombre"}, message="Ya existe un diagnostico con ese nombre.")
 */
class Diagnostico
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
private $id;

// etc...
}

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dsbifvcxr458755493 2014-01-18 00:19
    关注

    It doesn't return the Controller (it's not possible), the reason why you think it does is the behavior of the function get_class().

    Quoting the PHP documentation of the function get_class(): "If object is omitted when inside a class, the name of that class is returned.".

    Basically, in your case the find method returns an empty value therefore the entity is not found.

    When the current class is being returned by the function get_class() then you should try the function gettype(); this function will indicate you whether the value returned is a string, an object, NULL or any other types.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数