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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用