Skip to content
Advertisement

Trying to join 2 different entities in different bundles, failed

I’m trying to launch a query in Symfony2 (I’m quite new), where I need to join two different entities, in different bundles:

  • Candc/ComercioBundle/Entity/Venta/ItemVentaCarta And
  • Candc/ProductoBundle/Entity/Producto.

They have a relation manytoone-onetomany.

Class Producto/////
    /**
     *
     * @ORMId
     * @ORMColumn(name="id", type="integer")
     * @ORMOneToMany(targetEntity="CandcComercioBundleVentaItemVentaCarta", mappedBy="Producto")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

And:

Class ItemVentaCarta//////

    /**
     * catalog card wich is referenced.
     * @ORMManyToOne(targetEntity="CandcProductoBundleEntityProducto", inversedBy="ItemVentaCarta")
     * @ORMJoinColumn(name="carta_id", referencedColumnName="id", nullable=false)
     */ 
    private $carta;

This is the query I’m launching:

public function findLastProducts(){
//this is what I need to do in SQL language :
            $consulta = 'SELECT * FROM c_venta_item 
                LEFT JOIN c_venta_item_carta 
                ON c_venta_item.id=c_venta_item_carta.id
                LEFT JOIN usuario ON c_venta_item.user_id = usuario.id
                LEFT JOIN producto ON c_venta_item_carta.carta_id = producto.id';
            return $this->getEntityManager()
                ->createQuery("SELECT ivc
                    FROM CandcComercioBundleEntityVentaItemVentaCarta ivc
                    LEFT JOIN ivc.producto p
                    WHERE ivc.carta = p.id")
                ->getResult();
    }

I’m in Symfony 2.7.7 and the exception I get is that one:

[Semantical Error] line 0, col 105 near ‘p WHERE’: Error: Class CandcComercioBundleEntityVentaItemVentaCarta has no association named producto

(I tried both producto and Producto, to avoid Typo errors) Also searched in the forum, founded many post related, but cant solve it.

I cleared cache, and also tried a schema update, but I get a message that says:

“there’s nothing to update buddy, your db is already sync with the entity metadata”

Advertisement

Answer

Hi DQL is bound to the object properties, not the mapped entity name, please update your code by doing the following:

Class ItemVentaCarta//////

    /**
     * catalog card wich is referenced.
     * @ORMManyToOne(targetEntity="CandcProductoBundleEntityProducto", inversedBy="ItemVentaCarta")
     * @ORMJoinColumn(name="carta_id", referencedColumnName="id", nullable=false)
     */ 
    private $producto; // previously $carta

Also, it looks like your query needs changing to:

SELECT ivc 
FROM CandcComercioBundleEntityVentaItemVentaCarta ivc
LEFT JOIN ivc.producto p ON ivc.carta = p.id
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement