【问题标题】:Trying to join 2 different entities in different bundles, failed尝试在不同的捆绑包中加入 2 个不同的实体,失败
【发布时间】:2016-08-02 16:35:36
【问题描述】:

我正在尝试在 Symfony2 中启动一个查询(我是新手),我需要在不同的包中加入两个不同的实体:

  • Candc/ComercioBundle/Entity/Venta/ItemVentaCarta 和
  • Candc/ProductoBundle/Entity/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,以避免错字) 也在论坛里搜索过,找了很多相关的帖子,都解决不了。

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

“没有什么可以更新的,你的数据库已经和实体元数据同步了”

【问题讨论】:

    标签: php sql symfony doctrine


    【解决方案1】:

    您好,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
    

    【讨论】:

    • 感谢您的回答,我按照您的建议做了,查询的异常消失了,我将查询的结果返回给控制器,并将其传递给 Twig 系统,当我尝试编写实体产品的属性(链接到 ItemVentaCarta),例如名称,我仍然遇到异常:CandcFrontendBundle 中不存在对象“Candc\ComercioBundle\Entity\Venta\ItemVentaCarta”的方法“nombre”:第 346 行的 Portada:index.html.twig 确实,ItemVentaCarta 上不存在方法 nombre(名称),但它在 Product 中存在,我做错了什么?
    • 您需要确保您的实体中有一个 getter 和 setter。然后,您需要从 ItemVentaCarta 实体中获取产品实体(例如:$itemVentaCarta->getProducto()->setNombre(1);)
    猜你喜欢
    • 2012-02-19
    • 2012-03-08
    • 1970-01-01
    • 2018-11-22
    • 2012-12-09
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多