2013-03-02 48 views
3

我在Symfony2中有以下实体,一个产品实体和一个评论实体。Symfony2 ORM实体,ManyToOne双向

产品实体:

/** 
* @ORM\Entity 
* @ORM\Table(name="product") 
*/ 
class Product 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 
/** 
* @ORM\Column(type="string", length=100) 
*/ 
protected $name; 

的意见实体:

/** 
* @ORM\Entity 
* @ORM\Table(name="productComment") 
*/ 
class ProductComment 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
    /** 
    * @ORM\ManyToOne(targetEntity="Acme\ProductsBundle\Entity\Product", inversedBy="comments") 
    * @ORM\JoinColumn(name="product_id", referencedColumnName="id") 
    */ 
    protected $product; 
} 

我的问题是,我不知道如何从一个产品对象的意见。

回答

4

你必须一个comments属性添加到Product实体:

/** 
* @ORM\OneToMany(targetEntity="Acme\ProductsBundle\Entity\ProductComment", mappedBy="product") 
*/ 
private $comments; 

然后用

$product->getComments(); 
+0

感谢Hasts它的工作原理 – 2013-03-03 00:34:43