Symfony:一对多双向关系
Symfony: OneToMany Bidirectionnal Relation
这是我的实体 "Product" :
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() return null 但在数据库中没有 null。
请帮帮我...请...
在您的 Product
实体中,您缺少第二个 *
字符,该字符引入了包含 $offers
:
映射信息的文档块
/*
* @ORM\OneToMany(targetEntity="ShopBundle\Entity\Offer", mappedBy="product", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
private $offers;
因此,PHP 将这些行视为普通注释而不是文档块,这意味着 Doctrine 对它们一无所知。
把它改成这样:
class Product
{
// ...
/**
* @ORM\OneToMany(targetEntity="ShopBundle\Entity\Offer", mappedBy="product", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
private $offers;
// ...
}
这是我的实体 "Product" :
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() return null 但在数据库中没有 null。
请帮帮我...请...
在您的 Product
实体中,您缺少第二个 *
字符,该字符引入了包含 $offers
:
/*
* @ORM\OneToMany(targetEntity="ShopBundle\Entity\Offer", mappedBy="product", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
private $offers;
因此,PHP 将这些行视为普通注释而不是文档块,这意味着 Doctrine 对它们一无所知。
把它改成这样:
class Product
{
// ...
/**
* @ORM\OneToMany(targetEntity="ShopBundle\Entity\Offer", mappedBy="product", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
private $offers;
// ...
}