【问题标题】:Symfony 2 - Twig can't access to correct mapped entity objectsSymfony 2 - Twig 无法访问正确的映射实体对象
【发布时间】:2013-07-10 15:00:27
【问题描述】:

我是 Symfony 的新手

在我的控制器类中有一个简单的方法 indexAction:

    function indexAction()
    {
        $em = $this->getDoctrine()
            ->getEntityManager();

        $prods = $em->getRepository('EcommerceProductBundle:ProductData')->findBy(array('product'=>2));

        return $this->render('EcommerceProductBundle:Page:index.html.twig', array(
            'prods' => $prods
        )); 
    }

看起来效果很好,实际上在 Profiles 中可以看到查询,并且可以很好地检索到实体。

Ecommerce\ProductBundle\Entity\ProductData  Valid
Ecommerce\ProductBundle\Entity\ProductData  Valid
Ecommerce\ProductBundle\Entity\Product  Valid
Ecommerce\ProductBundle\Entity\Language     Valid
Ecommerce\ProductBundle\Entity\ProductImage     Valid
Ecommerce\ProductBundle\Entity\FileImage    Valid
Ecommerce\ProductBundle\Entity\Product  Valid
Ecommerce\ProductBundle\Entity\Language     Valid 

根据同一篇文章 Symfony 2 - Access mapped Object property form twig

我试图在我的树枝模板 (index.html.twig) 中调用 Product 实体的对象,如下所示

{% for prod in prods %}
    Price: {{ prod.Product.price }}
{% endfor %}

而且它工作正常。 如果我尝试调用 ProductImage 实体的对象,如下所示

{% for prod in prods %}
    Image title: prod.ProductImage.title
or

    {% for pimg in prod.ProductImage %}
{% endfor %}

我收到此错误:

Method "ProductImage" for object "Ecommerce\ProductBundle\Entity\ProductData" does not exist in EcommerceProductBundle:Page:index.html.twig at line 36 

ProductImage 实体Product 实体 一样被正确映射,为什么最后一个与第一个相反?

【问题讨论】:

  • 来自链接的示例:“请务必检查所有是否已链接到图像对象的帖子实体。如果一个实体帖子没有指向图像的链接,则您希望获得属性未找到错误尝试呈现您的页面时。”向我们展示您的 ProductData 和 Product 实体。

标签: php symfony entity twig template-engine


【解决方案1】:

我解决了调用 ProductData 中指向 ProductImage 实体的对象。

ProductData实体的

sn-p

 /**
 * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="product_pi", fetch="EAGER", cascade={"persist"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="product_id", referencedColumnName="product_id"),
 *   @ORM\JoinColumn(name="language_id", referencedColumnName="language_id")
 * })
 */
protected $products_pi;
index.html.twig 模板的

sn-p

{% for pimg in prod.getProductsPi %}
 {{ pimg.title }}
{% endfor %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2013-03-21
    • 2021-08-23
    • 1970-01-01
    相关资源
    最近更新 更多