简体   繁体   中英

Especific Query in Symfony2

i have an entity with my products, an i have a relation of one to many to another entity with the types and prices for each product. In my galery view, i want to bring all the products with their types and prices respectively, to do something like this:

{% for product in products%}
    <h2>{{ product.name }}</h2>
    <p>Description: {{ product.description }}</p>

    <table>
        <tr>

            {% for type in types %}

                <td>{{ type.type }}</td>

            {% endfor %}

         </tr>

         <tr>
             {% for type in types %}

                <td>{{ type.price }}</td>

             {% endfor %}
         </tr>
    </table>
{% endfor %}

i think that i have to do only one query, where i find all the products with their prices and types. But I do not know how.

i have exactly this:

Catalogo.php

Class Catalogo {

/**
     * @var string
     *
     * @ORM\Column(name="articulo", type="string", length=70, unique = true)
     */
    private $articulo;

/**
     * @ORM\OneToMany(targetEntity="Unidades", mappedBy="catalogo")
     */
    protected $unidades;

Medidas.php

Class Medidas{

/**
     * @var float
     *
     * @ORM\Column(name="medida", type="float")
     */
    private $medida;

    /**
     * @var float
     *
     * @ORM\Column(name="precio", type="float")
     */
    private $precio;

    /**
     * @ORM\ManyToOne(targetEntity="Catalogo", inversedBy="unidades", cascade={"persist"})
     * @ORM\JoinColumn(name="catalogo_id", referencedColumnName="id")
     */
    protected $catalogo;

And i do this, but it doesnt work:

public function findByArticuloYPrecios()
    {
        $em = $this->getEntityManager();
        $repository = $em->getRepository('ProyectoAdminBundle:Catalogo');
        $query = $repository->createQueryBuilder('Catalogo')
        ->select("Catalogo, Unidades")
        ->from("Catalogo", "catalogo")
        ->leftJoin("catalogo.unidades", "medida")
        ->getQuery();

        try 
        {
            return $query->getResult();
        }


        catch (\Doctrine\ORM\NoResultException $e) {
            return null;
        }
    }

The function return null.

If you have for example:

Product and some types in relation try to do this just like this:

 $query = $repository->createQueryBuilder('Catalogo')
    ->select("catalogo, unidades")
    ->from("Catalogo", "catalogo")
    ->leftJoin("catalogo.unidades", "unidades")
    ->getQuery()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM