尝试在不同的捆绑包中加入2个不同的实体,但失败了

时间:2016-04-12 07:52:28

标签: php sql symfony doctrine

早上,遇到这种情况: 我试图在Symfony2中启动一个查询(我很新),我需要在不同的包中加入2个不同的实体。 Candc / ComercioBundle / Entity / Venta / ItemVentaCarta和 Candc / ProductoBundle /实体/ PRODUCTO。

他们之间有很多关系。

Class Producto/////
    /**
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\OneToMany(targetEntity="Candc\ComercioBundle\Venta\ItemVentaCarta", mappedBy="Producto")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

并且:

Class ItemVentaCarta//////

    /**
     * catalog card wich is referenced.
     * @ORM\ManyToOne(targetEntity="\Candc\ProductoBundle\Entity\Producto", inversedBy="ItemVentaCarta")
     * @ORM\JoinColumn(name="carta_id", referencedColumnName="id", nullable=false)
     */ 
    private $carta;

这是我启动的查询:

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 \Candc\ComercioBundle\Entity\Venta\ItemVentaCarta ivc
                    LEFT JOIN ivc.producto p
                    WHERE ivc.carta = p.id")
                ->getResult();
    }

我在Symfony 2.7.7中,我得到的例外是:

[语义错误]第0行,第105行靠近'p WHERE':错误:类Candc \ ComercioBundle \ Entity \ Venta \ ItemVentaCarta没有名为producto的关联

(我尝试过producto和Producto,以避免Typo错误) 也在论坛中搜索,创建了许多相关的帖子,但无法解决它。

我清除了缓存,并尝试了架构更新,但我收到一条消息:

"theres nothing to update buddy, your db is already sync with the entity metadata"

感谢任何帮助,如果英语失败,感谢对不起,谢谢你们!

1 个答案:

答案 0 :(得分:0)

您好DQL绑定到对象属性,而不是映射的实体名称,请通过执行以下操作更新您的代码:

Class ItemVentaCarta//////

    /**
     * catalog card wich is referenced.
     * @ORM\ManyToOne(targetEntity="\Candc\ProductoBundle\Entity\Producto", inversedBy="ItemVentaCarta")
     * @ORM\JoinColumn(name="carta_id", referencedColumnName="id", nullable=false)
     */ 
    private $producto; // previously $carta

此外,您的查询似乎需要更改为:

SELECT ivc 
FROM \Candc\ComercioBundle\Entity\Venta\ItemVentaCarta ivc
LEFT JOIN ivc.producto p ON ivc.carta = p.id