gpt4 book ai didi

Symfony2 ORM 实体,ManyToOne 双向

转载 作者:行者123 更新时间:2023-12-02 17:12:21 25 4
gpt4 key购买 nike

我在 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;
}

我的问题是我不知道如何从产品对象中获取评论。

最佳答案

您必须将 comments 属性添加到 Product 实体中:

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

然后使用

$product->getComments();

关于Symfony2 ORM 实体,ManyToOne 双向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15180447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com