phpsymfonyinheritancedoctrine-ormmappingexception

Symfony2 - Doctrine exception: class used in the discriminator map does not exist


I am using Symfony2 with Doctrine and when I query from my controller the next error appears(it appears in the navigator when I call for the page):

Entity class 'Bdreamers\SuenoBundle\Entity\Sueno_video' used in the discriminator map of class 'Bdreamers\SuenoBundle\Entity\Sueno' does not exist.

I have one entity(superclass) called "Sueno" and two entities that extend from it(subclasses): Sueno_foto and Sueno_video.

When I load the fixtures, Doctrine works perfectly and fills the database without any issue, filling correctly the discriminator field "tipo" in the "Sueno" table. It also fills correctly the inherited entity table "Sueno_video" introducing the ID of "Sueno" and the exclusive fields of "Sueno_video"

This is the code of the entity file for "Sueno":

<?php

namespace Bdreamers\SuenoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table()
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="tipo", type="string")
 * @ORM\DiscriminatorMap({"sueno" = "Sueno", "video" = "Sueno_video", "foto" = "Sueno_foto"})
 */
class Sueno
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario")
     **/
    private $usuario;

    /**
     * @ORM\ManyToMany(targetEntity="Bdreamers\SuenoBundle\Entity\Tag", inversedBy="suenos")
     * @ORM\JoinTable(name="suenos_tags")
     **/
    private $tags;

    /**
     * @ORM\ManyToMany(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario", mappedBy="suenos_sigue")
     * @ORM\JoinTable(name="usuarios_siguen")
     **/
    private $usuariosSeguidores;

    /**
     * @ORM\ManyToMany(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario", mappedBy="suenos_colabora")
     * @ORM\JoinTable(name="usuarios_colaboran")
     **/
    private $usuariosColaboradores;


    /**
     * @var \DateTime
     *
     * @ORM\Column(name="fecha_subida", type="datetime")
     */
    private $fechaSubida;

    /**
     * @var string
     *
     * @ORM\Column(name="titulo", type="string", length=40)
     */
    private $titulo;

    /**
     * @var string
     *
     * @ORM\Column(name="que_pido", type="string", length=140)
     */
    private $quePido;

    /**
     * @var string
     *
     * @ORM\Column(name="texto", type="string", length=540)
     */
    private $texto;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set usuario
     *
     * @param string $usuario
     * @return Sueno
     */
    public function setUsuario($usuario)
    {
        $this->usuario = $usuario;

        return $this;
    }

    /**
     * Get usuario
     *
     * @return string 
     */
    public function getUsuario()
    {
        return $this->usuario;
    }

    public function getTags()
    {
        return $this->tags;
    }

    /**
     * Set usuariosSeguidores
     *
     * @param string $usuariosSeguidores
     * @return Sueno
     */
    public function setUsuariosSeguidores($usuariosSeguidores)
    {
        $this->usuariosSeguidores = $usuariosSeguidores;

        return $this;
    }

    /**
     * Get usuariosSeguidores
     *
     * @return string 
     */
    public function getUsuariosSeguidores()
    {
        return $this->usuariosSeguidores;
    }

    /**
     * Set usuariosColaboradores
     *
     * @param string $usuariosColaboradores
     * @return Sueno
     */
    public function setUsuariosColaboradores($usuariosColaboradores)
    {
        $this->usuariosColaboradores = $usuariosColaboradores;

        return $this;
    }

    /**
     * Get usuariosColaboradores
     *
     * @return string 
     */
    public function getUsuariosColaboradores()
    {
        return $this->usuariosColaboradores;
    }


    /**
     * Set fechaSubida
     *
     * @param \DateTime $fechaSubida
     * @return Sueno
     */
    public function setFechaSubida($fechaSubida)
    {
        $this->fechaSubida = $fechaSubida;

        return $this;
    }

    /**
     * Get fechaSubida
     *
     * @return \DateTime 
     */
    public function getFechaSubida()
    {
        return $this->fechaSubida;
    }

    /**
     * Set titulo
     *
     * @param string $titulo
     * @return Sueno
     */
    public function setTitulo($titulo)
    {
        $this->titulo = $titulo;

        return $this;
    }

    /**
     * Get titulo
     *
     * @return string 
     */
    public function getTitulo()
    {
        return $this->titulo;
    }

    /**
     * Set quePido
     *
     * @param string $quePido
     * @return Sueno
     */
    public function setQuePido($quePido)
    {
        $this->quePido = $quePido;

        return $this;
    }

    /**
     * Get quePido
     *
     * @return string 
     */
    public function getQuePido()
    {
        return $this->quePido;
    }

    /**
     * Set texto
     *
     * @param string $texto
     * @return Sueno
     */
    public function setTexto($texto)
    {
        $this->texto = $texto;

        return $this;
    }

    /**
     * Get texto
     *
     * @return string 
     */
    public function getTexto()
    {
        return $this->texto;
    }

    public function __construct() {
        $this->usuariosColaboradores = new \Doctrine\Common\Collections\ArrayCollection();
        $this->usuariosSeguidores = new \Doctrine\Common\Collections\ArrayCollection();
        $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
    }

        public function __toString()
        {
            return $this->getTitulo();
        }
}

And this is the code for the entity Sueno_video:

<?php

namespace Bdreamers\SuenoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table()
 * @ORM\Entity
 */
class Sueno_video extends Sueno
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="link_video", type="string", length=255)
     */
    private $linkVideo;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set linkVideo
     *
     * @param string $linkVideo
     * @return Sueno_video
     */
    public function setLinkVideo($linkVideo)
    {
        $this->linkVideo = $linkVideo;

        return $this;
    }

    /**
     * Get linkVideo
     *
     * @return string 
     */
    public function getLinkVideo()
    {
        return $this->linkVideo;
    }
}

And finally the code in the controller:

public function homeAction()
{

    $em = $this->getDoctrine()->getManager();

    $suenos = $em->getRepository('SuenoBundle:Sueno')->findOneBy(array(
        'fechaSubida' => new \DateTime('now -2 days')
        ));

    return $this->render('EstructuraBundle:Home:home_registrado.html.twig');
}

Solution

  • The autoloader won't be able to resolve those class names to file paths, hence why it can't find your classes.

    Changing the file and class names to SuenoVideo and SuenoFoto.