Symfony 2-Twig can';t访问正确映射的实体对象


Symfony 2 - Twig can't access to correct mapped entity objects

我是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
        )); 
    }

看起来效果很好——事实上,概要文件中有可见的查询,实体也得到了很好的检索。

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-访问映射的对象属性表单树枝

我试着在我的小树枝模板(index.html.titch)中调用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实体产品实体

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

ProductData实体的片段

 /**
 * @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.titch模板的片段

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