【问题标题】:Symfony2 ORM Entities, ManyToOne bidirectionalSymfony2 ORM 实体,ManyToOne 双向
【发布时间】:2023-03-24 09:39:01
【问题描述】:

我在 Symfony2 中有以下实体,一个产品实体和一个 cmets 实体。

产品实体:

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

cmets 实体:

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

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

【问题讨论】:

    标签: symfony doctrine many-to-one


    【解决方案1】:

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

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

    然后使用

    $product->getComments();
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多