我的问题是,当我尝试使用$em->find
方法查找数据库记录时,它会返回一个控制器。
我举个例子:
NeostatDiagnosticoBundleControllerComponentController.php
:
$em = $this->getDoctrine()->getEntityManager();
$diagnostico = $em->getRepository('NeostatDiagnosticoBundle:Diagnostico')->find($id);
var_dump(get_class($diagnostico));
它返回NeostatDiagnosticoBundleControllerComponentController
.
但是我在src/Neostat/DiagnosticoBundle/Entity/Diagnostico.php
中有一个名为Diagnostico.php
的实体:
namespace NeostatDiagnosticoBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use NeostatPacienteBundleEntityPaciente;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* Diagnostico
*
* @ORMTable(name="diagnostico")
* @ORMEntity(repositoryClass="NeostatDiagnosticoBundleEntityDiagnosticoRepository")
* @UniqueEntity(fields={"nombre"}, message="Ya existe un diagnostico con ese nombre.")
*/
class Diagnostico
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
// etc...
}
我做错了什么?
它不返回控制器(这是不可能的),您认为它返回的原因是函数的行为get_class()
。
引用函数 get_class() 的 PHP 文档:"如果在类中省略object
,则返回该类的名称。
基本上,在您的情况下,find
方法返回一个空值,因此找不到实体。
当函数返回当前类时get_class()
你应该尝试函数gettype();这个函数将指示返回的值是字符串,对象,NULL 还是任何其他类型。
对于查找教义数据库记录,请使用findOneById或喜欢findOneByUser等。
如果你想找到一个列表,使用findByField,如findByType。
这些是规定违约的学说。