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

Fed*_*enz 1 php doctrine entitymanager symfony

我的问题是,当我尝试使用$em->find方法查找数据库记录时,它返回一个Controller.

让我举一个例子:

Neostat\DiagnosticoBundle\Controller\ComponentController.php:

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

它回来了Neostat\DiagnosticoBundle\Controller\ComponentController.

但我有一个名为实体Diagnostico.phpsrc/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...
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Tho*_*ire 5

它不会返回Controller(这是不可能的),你认为它的作用是因为函数的行为get_class().

引用函数get_class()PHP文档:"如果object在类中省略,则返回该类的名称."

基本上,在您的情况下,该find方法返回一个空值,因此找不到该实体.

当函数返回当前类时,get_class()你应该尝试函数gettype() ; 此函数将指示返回的值是字符串,对象,NULL还是任何其他类型.

  • 考虑更改`$ diagnostico = $ em-> getRepository('NeostatDiagnosticoBundle:Diagnostico') - > find($ id); var_dump(get_class($ diagnostico));`with` $ diagnostico = $ em-> getRepository('NeostatDiagnosticoBundle:Diagnostico') - > find($ id); if(!$ diagnostico){throw $ this-> createNotFoundException(); }` (2认同)