Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 ORM Entities, ManyToOne bidirectional

i have the following entities in Symfony2, a products entity and a comments entity.

The products entity:

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

The comments entity:

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

My problem it's that i don't know how to get the comments from a product objects.

like image 404
Nicolas Ardison Avatar asked Dec 29 '25 18:12

Nicolas Ardison


1 Answers

You have to add a comments property into the Product entity:

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

And then use

$product->getComments();
like image 151
Hast Avatar answered Jan 02 '26 03:01

Hast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!