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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21196375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:53:54  来源:igfitidea点击:

EntityManager getRepository, method find(id) returns controller

phpsymfonydoctrineentitymanager

提问by Federico Saenz

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

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

Let me put an example:

我举个例子:

Neostat\DiagnosticoBundle\Controller\ComponentController.php:

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.

它返回Neostat\DiagnosticoBundle\Controller\ComponentController

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

但我有一个名为实体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...
}

What am I doing wrong?

我究竟做错了什么?

回答by Thomas Potaire

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().

它不返回控制器(这是不可能的),你认为它这样做的原因是函数的行为get_class()

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

引用函数 get_class()PHP 文档:“如果object在类内部省略,则返回该类的名称。”。

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

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

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.

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

回答by thewbb

for find doctrine database record, please using findOneById or like findOneByUser etc..

要查找学说数据库记录,请使用 findOneById 或 findOneByUser 等。

if you want to find a list, using findByField like findByType.

如果要查找列表,请使用 findByField 之类的 findByType。

these is doctrine provide for default.

这些是对违约的规定。