Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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

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

these is doctrine provide for default.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top