Foros del Web » Programando para Internet » PHP » Symfony »

[SOLUCIONADO] Consulta Dql

Estas en el tema de Consulta Dql en el foro de Symfony en Foros del Web. Buenas otra vez, primero de todo dar las gracias por la ayuda. Llevo rato con esta consulta con JOIN pero no hay forma que me ...
  #1 (permalink)  
Antiguo 04/03/2016, 08:25
 
Fecha de Ingreso: marzo-2012
Mensajes: 180
Antigüedad: 12 años
Puntos: 2
Consulta Dql

Buenas otra vez,

primero de todo dar las gracias por la ayuda.

Llevo rato con esta consulta con JOIN pero no hay forma que me salga.

Esta es la consulta en SQL, me gustaría convertirla en DQL.

Código SQL:
Ver original
  1. SELECT *
  2. FROM installations_companies
  3. JOIN districts_brasil
  4. ON districts_brasil.id = installations_companies.district_id
  5. JOIN states_brasil
  6. ON states_brasil.id = installations_companies.state_id

Consulta en DQL

Código SQL:
Ver original
  1. SELECT i, d, s
  2. FROM VnfqInstComBundle:InstCom i
  3. JOIN VnfqInstComBundle:District d
  4. WHERE d.id = :district_id
  5. JOIN VnfqInstComBundle:State s
  6. WHERE s.id = :state_id
  #2 (permalink)  
Antiguo 04/03/2016, 08:50
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Consulta Dql

Quieres obtener todas las compañías con sus distritos y estado ?
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #3 (permalink)  
Antiguo 04/03/2016, 09:26
 
Fecha de Ingreso: marzo-2012
Mensajes: 180
Antigüedad: 12 años
Puntos: 2
Respuesta: Consulta Dql

Buenas,

pero tengo que hacerlo con un JOIN porque en tabla compañía tengo el ID y quiero el nombre del distrito.

La consulta en el PHPMYADMIN funciona correctamente, pero en la Clase de Symfony pues no me va.

Última edición por manelmanel8; 04/03/2016 a las 09:36
  #4 (permalink)  
Antiguo 04/03/2016, 12:27
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Consulta Dql

puedes mostrar las relaciones entre esas entidades ?
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #5 (permalink)  
Antiguo 04/03/2016, 15:12
 
Fecha de Ingreso: marzo-2012
Mensajes: 180
Antigüedad: 12 años
Puntos: 2
Respuesta: Consulta Dql

Las entidades que tengo en symfony o el model entidad relación de la base de datos?

El martes pongo lo que me pides que estoy fuera de casa unos días.

Ya me dices qual de las dos cosas o las dos. No hay ningún problema.

Por cierto, gracias por tu tiempo.
  #6 (permalink)  
Antiguo 05/03/2016, 18:30
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Consulta Dql

No, solo las entidades que tienes en symfony quiero ver como hiciste las relaciones entre ellas
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #7 (permalink)  
Antiguo 08/03/2016, 06:10
 
Fecha de Ingreso: marzo-2012
Mensajes: 180
Antigüedad: 12 años
Puntos: 2
Respuesta: Consulta Dql

Aquí tienes las 3 entidades.

Te lo pongo en 2 mensajes porque no me deja poner la respuesta más larga de 10000 caracteres.

InstCom.php
Código PHP:
<?php

namespace VnfqInstComBundleEntity
;

use 
DoctrineORMMapping as ORM;

/**
 * InstCom
 *
 * @ORM\Table(name="installations_companies")
 * @ORM\Entity(repositoryClass="Vnfq\InstComBundle\Repository\InstComRepository")
 */
class InstCom
{   
    
/**
     * @ORM\ManyToOne(targetEntity="District", inversedBy="instcom")
     * @ORM\JoinColumn(name="district_id", referencedColumnName="id")
     */
    
protected $district;
    
    
/**
     * @ORM\ManyToOne(targetEntity="State", inversedBy="instcom")
     * @ORM\JoinColumn(name="state_id", referencedColumnName="id")
     */
    
protected $state;
    
    
/**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    
private $id;

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

    
/**
     * @var string
     *
     * @ORM\Column(name="cnpj", type="string", length=50, nullable=true)
     */
    
private $cnpj;

    
/**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=100, nullable=true)
     */
    
private $email;

    
/**
     * @var string
     *
     * @ORM\Column(name="phone", type="string", length=50, nullable=true)
     */
    
private $phone;

    
/**
     * @var string
     *
     * @ORM\Column(name="cell_phone", type="string", length=50, nullable=true)
     */
    
private $cellPhone;

    
/**
     * @var int
     *
     * @ORM\Column(name="district_id", type="integer")
     */
    
private $district_id;

    
/**
     * @var int
     *
     * @ORM\Column(name="city", type="string", length=100, nullable=true)
     */
    
private $city;

    
/**
     * @var int
     *
     * @ORM\Column(name="state_id", type="integer")
     */
    
private $state_id;

    
/**
     * @var string
     *
     * @ORM\Column(name="country", type="string", length=100, nullable=true)
     */
    
private $country;


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

    
/**
     * Set name
     *
     * @param string $name
     * @return InstCom
     */
    
public function setName($name)
    {
        
$this->name $name;

        return 
$this;
    }

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

    
/**
     * Set cnpj
     *
     * @param string $cnpj
     * @return InstCom
     */
    
public function setCnpj($cnpj)
    {
        
$this->cnpj $cnpj;

        return 
$this;
    }

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

    
/**
     * Set email
     *
     * @param string $email
     * @return InstCom
     */
    
public function setEmail($email)
    {
        
$this->email $email;

        return 
$this;
    }

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

    
/**
     * Set phone
     *
     * @param string $phone
     * @return InstCom
     */
    
public function setPhone($phone)
    {
        
$this->phone $phone;

        return 
$this;
    }

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

    
/**
     * Set cellPhone
     *
     * @param string $cellPhone
     * @return InstCom
     */
    
public function setCellPhone($cellPhone)
    {
        
$this->cellPhone $cellPhone;

        return 
$this;
    }

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

    
/**
     * Set district_id
     *
     * @param string $district_id
     * @return InstCom
     */
    
public function setDistrict($district_id)
    {
        
$this->district_id $district_id;

        return 
$this;
    }

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

    
/**
     * Set city
     *
     * @param integer $city
     * @return InstCom
     */
    
public function setCity($city)
    {
        
$this->city $city;

        return 
$this;
    }

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

    
/**
     * Set state_id
     *
     * @param integer $state_id
     * @return InstCom
     */
    
public function setState($state_id)
    {
        
$this->state_id $state_id;

        return 
$this;
    }

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

    
/**
     * Set country
     *
     * @param string $country
     * @return InstCom
     */
    
public function setCountyr($country)
    {
        
$this->country $country;

        return 
$this;
    }

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

    
/**
     * Set district_id
     *
     * @param integer $districtId
     * @return InstCom
     */
    
public function setDistrictId($districtId)
    {
        
$this->district_id $districtId;

        return 
$this;
    }

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

    
/**
     * Set state_id
     *
     * @param integer $stateId
     * @return InstCom
     */
    
public function setStateId($stateId)
    {
        
$this->state_id $stateId;

        return 
$this;
    }

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

    
/**
     * Set country
     *
     * @param string $country
     * @return InstCom
     */
    
public function setCountry($country)
    {
        
$this->country $country;

        return 
$this;
    }

    
/**
     * Get country
     *
     * @return string 
     */
    
public function getCountry()
    {
        return 
$this->country;
    }
}
  #8 (permalink)  
Antiguo 08/03/2016, 06:11
 
Fecha de Ingreso: marzo-2012
Mensajes: 180
Antigüedad: 12 años
Puntos: 2
Respuesta: Consulta Dql

District.php
Código PHP:
<?php

namespace VnfqInstComBundleEntity
;

use 
DoctrineORMMapping as ORM;
use 
DoctrineCommonCollectionsArrayCollection;

/**
 * District
 *
 * @ORM\Table(name="districts_brasil")
 * @ORM\Entity(repositoryClass="Vnfq\InstComBundle\Repository\DistrictRepository")
 */
class District
{
    
/**
     * @ORM\OneToMany(targetEntity="InstCom", mappedBy="district")
     */
    
protected $instCom;
    
    public function 
__construct()
    {
        
$this->instCom = new ArrayCollection();
    }
    
    
/**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    
private $id;

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


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

    
/**
     * Set name
     *
     * @param string $name
     * @return District
     */
    
public function setName($name)
    {
        
$this->name $name;

        return 
$this;
    }

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

    
/**
     * Add instCom
     *
     * @param \Vnfq\InstComBundle\Entity\InstCom $instCom
     * @return District
     */
    
public function addInstCom(VnfqInstComBundleEntityInstCom $instCom)
    {
        
$this->instCom[] = $instCom;

        return 
$this;
    }

    
/**
     * Remove instCom
     *
     * @param \Vnfq\InstComBundle\Entity\InstCom $instCom
     */
    
public function removeInstCom(VnfqInstComBundleEntityInstCom $instCom)
    {
        
$this->instCom->removeElement($instCom);
    }

    
/**
     * Get instCom
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    
public function getInstCom()
    {
        return 
$this->instCom;
    }
}
State.php
Código PHP:
<?php

namespace VnfqInstComBundleEntity
;

use 
DoctrineORMMapping as ORM;
use 
DoctrineCommonCollectionsArrayCollection;

/**
 * State
 *
 * @ORM\Table(name="states_brasil")
 * @ORM\Entity(repositoryClass="Vnfq\InstComBundle\Repository\StateRepository")
 */
class State
{   
    
/**
     * @ORM\OneToMany(targetEntity="InstCom", mappedBy="state")
     */
    
protected $instCom;
    
    public function 
__construct()
    {
        
$this->instCom = new ArrayCollection();
    }
    
    
/**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    
private $id;

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

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


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

    
/**
     * Set initials
     *
     * @param string $initials
     * @return State
     */
    
public function setInitials($initials)
    {
        
$this->initials $initials;

        return 
$this;
    }

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

    
/**
     * Set state
     *
     * @param string $state
     * @return State
     */
    
public function setState($state)
    {
        
$this->state $state;

        return 
$this;
    }

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

    
/**
     * Add instCom
     *
     * @param \Vnfq\InstComBundle\Entity\InstCom $instCom
     * @return State
     */
    
public function addInstCom(VnfqInstComBundleEntityInstCom $instCom)
    {
        
$this->instCom[] = $instCom;

        return 
$this;
    }

    
/**
     * Remove instCom
     *
     * @param \Vnfq\InstComBundle\Entity\InstCom $instCom
     */
    
public function removeInstCom(VnfqInstComBundleEntityInstCom $instCom)
    {
        
$this->instCom->removeElement($instCom);
    }

    
/**
     * Get instCom
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    
public function getInstCom()
    {
        return 
$this->instCom;
    }
}
  #9 (permalink)  
Antiguo 08/03/2016, 13:41
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Consulta Dql

Tu consulta debe de ser algo como esto:
Código PHP:
Ver original
  1. $em = $this->getDoctrine()->getManager();
  2.  
  3.         $query = $em->createQuery(
  4.             'SELECT i, d, s
  5.              FROM VnfqInstComBundle:InstCom i
  6.                JOIN VnfqInstComBundle:District d
  7.                JOIN VnfqInstComBundle:State s
  8.              WHERE i.id = :inst_id
  9.            '
  10.         )->setParameter('inst_Id', $id);
  11.  
  12.         $instCom = $query->getOneOrNullResult();
  13.  
  14.         $instCom->getCity();
  15.         $instCom->getState();

Si quieres que te regrese todos los InstCom con su Distrito y Estado entonces quitas el where y cambias el método getOneOrNullResult() por getResult()
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #10 (permalink)  
Antiguo 08/03/2016, 16:29
 
Fecha de Ingreso: marzo-2012
Mensajes: 180
Antigüedad: 12 años
Puntos: 2
Respuesta: Consulta Dql

No me funciona

Intento encontrar el error pero no hay forma. Te dejo el controlador y una imagen del error que me da.

Gracias por tu tiempo.



Código PHP:
<?php

namespace VnfqInstComBundleController
;

use 
SymfonyBundleFrameworkBundleControllerController;
use 
VnfqInstComBundleEntityInstCom;
use 
VnfqInstComBundleEntityDistrict;
use 
VnfqInstComBundleEntityState;
use 
VnfqInstComBundleFormInstComType;
use 
VnfqInstComBundleFormDistrictType;
use 
VnfqInstComBundleFormStateType;

class 
InstComController extends Controller
{
    public function 
indexAction()
    {
        
$em $this->getDoctrine()->getManager();
        
$id 1;
        
$query $em->createQuery(
            
'SELECT i, d, s
            FROM VnfqInstComBundle:InstCom i
            JOIN VnfqInstComBundle:District d
            JOIN VnfqInstComBundle:State s
              WHERE i.id = :inst_id'
        
)->setParameter('inst_id'$id);
     
        
$instCom $query->getOneOrNullResult();
     
        
$instCom->getCity();
        
$instCom->getState();
        
        
print_r($instCom);
        
        return 
$this->render('VnfqInstComBundle:Inst_com:index.html.twig'); 
    }
}
  #11 (permalink)  
Antiguo 08/03/2016, 17:01
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Consulta Dql

Mil disculpas cometi el mismo error remplaza por esto:
Código PHP:
Ver original
  1. $em = $this->getDoctrine()->getManager();
  2.  
  3.         $query = $em->createQuery(
  4.             'SELECT i, d, s
  5.              FROM VnfqInstComBundle:InstCom i
  6.                JOIN i.district d
  7.                JOIN i.state s
  8.              WHERE i.id = :inst_id
  9.            '
  10.         )->setParameter('inst_Id', $id);
  11.  
  12.         $instCom = $query->getOneOrNullResult();
No es necesaria esta parte
Código PHP:
Ver original
  1. $instCom->getCity();
  2.         $instCom->getState();
Es solo a modo de ejemplo de como harias las llamadas dese el objeto, desde twig usas el punto
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:45.