簡體   English   中英

Symfony:一對多雙向關系

[英]Symfony: OneToMany Bidirectionnal Relation

這是我的實體“產品”:

class Product
{
    /**
     * @var int
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /*
     * @ORM\OneToMany(targetEntity="ShopBundle\Entity\Offer", mappedBy="product", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
     */
    private $offers;

    /*
     * Constructeur
     */
    public function __construct()
    {
       $this->offers = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add offer
     * @param \ShopBundle\Entity\Offer $offer
     * @return Product
     */
    public function addOffer(\ShopBundle\Entity\Offer $offer)
    {
        $this->offers[] = $offer;
        return $this;
    }

    /**
     * Remove offer
     * @param \ShopBundle\Entity\Offer $offer
     */
    public function removeOffer(\ShopBundle\Entity\Offer $offer)
    {
        $this->offers->removeElement($offer);
    }

    /**
     * Get parameters
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getOffers()
    {
        return $this->offers;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}

而且,這是我的實體“ Offer”:

class Offer
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="ShopBundle\Entity\Product", inversedBy="offers", cascade={"persist", "merge"})
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
     */
    private $product;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set product
     *
     * @param \ShopBundle\Entity\Product $product
     *
     * @return Offer
     */
    public function setProduct(\ShopBundle\Entity\Product $product)
    {
        $this->product = $product;

        return $this;
    }

    /**
     * Get product
     *
     * @return \ShopBundle\Entity\Product
     */
    public function getProduct()
    {
        return $this->product;
    }
}

何時,我在控制器中使用$ product = $ em-> getRepository('ShopBundle:Product')-> find($ id);獲得對象。

然后,$ product-> getOffers()返回null,但在數據庫中不存在null。

請幫幫我 ...

在您的Product實體中,您缺少第二個*字符,該字符引入了包含$offers映射信息的docblock:

/*
 * @ORM\OneToMany(targetEntity="ShopBundle\Entity\Offer", mappedBy="product", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
 */
private $offers;

因此,PHP將這些行視為普通注釋,而不是docblock,這意味着Doctrine對它們一無所知。

只需將其更改為如下所示:

class Product
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="ShopBundle\Entity\Offer", mappedBy="product", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
     */
    private $offers;

    // ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM