3

Symfony2 には、製品エンティティとコメント エンティティの次のエンティティがあります。

製品エンティティ:

/**
 * @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

1 に答える 1

4

エンティティにcommentsプロパティを追加する必要があります。Product

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

そして、

$product->getComments();
于 2013-03-02T23:59:35.313 に答える