DDC-3543: How to map and use a DB View from Doctrine2 #4360

Closed
opened 2026-01-22 14:40:14 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 26, 2015).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user reynierpm:

I have a view on nomencladores schema called obtenerPaisesPorFabricanteProductoSolicitud. This is the content for the view:

    SELECT
        ps.id AS psid,
        ps.nombre,
        fps.id AS fpsid
    FROM
        (
            (
                nomencladores.pais ps
                JOIN nomencladores.pais*fabricante_producto_solicitud pfps ON ((pfps.pais*id = ps.id))
            )
            JOIN negocio.fabricante*producto*solicitud fps ON (
                (
                    pfps.fabricante*producto_solicitud*id = fps.id
                )
            )
        );

I'm trying to map the view as follow:

    use Doctrine\ORM\Mapping as ORM;

    /****
     * @ORM\Entity
     * @ORM\Table(name="nomencladores.obtenerPaisesPorFabricanteProductoSolicitud", schema="nomencladores")
     */
    class ObtenerPaisesPorFabricanteProductoSolicitud
    {
        /****
         * @ORM\Id
         * @ORM\Column(name="psid", type="integer", nullable=false, unique=true)
         */
        protected $ps;

        /****
         * @ORM\Column(name="fpsid", type="integer")
         */
        protected $fps;

        /****
         * @ORM\Column(name="nombre", type="string")
         */
        protected $nombre;

        public function getPs()
        {
            return $this->ps;
        }

        public function getFps()
        {
            return $this->fps;
        }

        public function getNombre()
        {
            return $this->nombre;
        }
    }

But any time I run this code on it:

    $ent = $em->getRepository("AppBundle:ObtenerPaisesPorFabricanteProductoSolicitud")->findBy(
        array(
            "fps" => $entF->getId()
        )
    );

I got this result:

{quote}
An exception occurred while executing 'SELECT t0.psid AS psid1,
t0.fpsid AS fpsid2, t0.nombre AS nombre3 FROM
nomencladores.obtenerPaisesPorFabricanteProductoSolicitud t0 WHERE
t0.fpsid = ?' with params [22]:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "nomencladores.obtenerpaisesporfabricanteproductosolicitud" does not
exist LINE 1: ...d1, t0.fpsid AS fpsid2, t0.nombre AS nombre3 FROM
nomenclado...
{quote}

If I remove the annotations then the error transform on this:

{quote}
Class "AppBundle\Entity\ObtenerPaisesPorFabricanteProductoSolicitud" is not a valid entity or mapped super class."
{quote}

Why Doctrine2 or Symfony tries to execute the query instead go through the view? How I can execute the view from Symfony2/Doctrine2 side?

Originally created by @doctrinebot on GitHub (Jan 26, 2015). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user reynierpm: I have a view on `nomencladores` schema called `obtenerPaisesPorFabricanteProductoSolicitud`. This is the content for the view: ``` SELECT ps.id AS psid, ps.nombre, fps.id AS fpsid FROM ( ( nomencladores.pais ps JOIN nomencladores.pais*fabricante_producto_solicitud pfps ON ((pfps.pais*id = ps.id)) ) JOIN negocio.fabricante*producto*solicitud fps ON ( ( pfps.fabricante*producto_solicitud*id = fps.id ) ) ); ``` I'm trying to map the view as follow: ``` use Doctrine\ORM\Mapping as ORM; /**** * @ORM\Entity * @ORM\Table(name="nomencladores.obtenerPaisesPorFabricanteProductoSolicitud", schema="nomencladores") */ class ObtenerPaisesPorFabricanteProductoSolicitud { /**** * @ORM\Id * @ORM\Column(name="psid", type="integer", nullable=false, unique=true) */ protected $ps; /**** * @ORM\Column(name="fpsid", type="integer") */ protected $fps; /**** * @ORM\Column(name="nombre", type="string") */ protected $nombre; public function getPs() { return $this->ps; } public function getFps() { return $this->fps; } public function getNombre() { return $this->nombre; } } ``` But any time I run this code on it: ``` $ent = $em->getRepository("AppBundle:ObtenerPaisesPorFabricanteProductoSolicitud")->findBy( array( "fps" => $entF->getId() ) ); ``` I got this result: {quote} An exception occurred while executing 'SELECT t0.psid AS psid1, t0.fpsid AS fpsid2, t0.nombre AS nombre3 FROM nomencladores.obtenerPaisesPorFabricanteProductoSolicitud t0 WHERE t0.fpsid = ?' with params [22]: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "nomencladores.obtenerpaisesporfabricanteproductosolicitud" does not exist LINE 1: ...d1, t0.fpsid AS fpsid2, t0.nombre AS nombre3 FROM nomenclado... {quote} If I remove the annotations then the error transform on this: {quote} Class "AppBundle\Entity\ObtenerPaisesPorFabricanteProductoSolicitud" is not a valid entity or mapped super class." {quote} Why Doctrine2 or Symfony tries to execute the query instead go through the view? How I can execute the view from Symfony2/Doctrine2 side?
admin added the Bug label 2026-01-22 14:40:14 +01:00
admin closed this issue 2026-01-22 14:40:14 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 26, 2015):

Comment created by @ocramius:

Nothing wrong here: the SQL generated by the ORM is correct, but your view definition is wrong: try running the select in console, manually.

@doctrinebot commented on GitHub (Jan 26, 2015): Comment created by @ocramius: Nothing wrong here: the SQL generated by the ORM is correct, but your view definition is wrong: try running the select in console, manually.
Author
Owner

@doctrinebot commented on GitHub (Jan 26, 2015):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jan 26, 2015): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4360