Symfony 2.8学说。使用复合主键和多个关联
我有两个实体-平台和产品。在Products表中,我有[Product ID+Platform ID]的复合主键。一个产品可以出现在多个平台上,因此一个平台可以包含多个产品,因此关联是很多的 平台实体:Symfony 2.8学说。使用复合主键和多个关联,symfony,doctrine,many-to-many,symfony-2.8,Symfony,Doctrine,Many To Many,Symfony 2.8,我有两个实体-平台和产品。在Products表中,我有[Product ID+Platform ID]的复合主键。一个产品可以出现在多个平台上,因此一个平台可以包含多个产品,因此关联是很多的 平台实体: /** * Platform * * @ORM\Table(name="Platforms") */ class Platform { /** * @var int * * @ORM\Column(name="Platform_Id", type=&
/**
* Platform
*
* @ORM\Table(name="Platforms")
*/
class Platform
{
/**
* @var int
*
* @ORM\Column(name="Platform_Id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Platform_Name", type="string", length=64)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="Product", mappedBy="platforms", cascade={"ALL"}, indexBy="numpp")
*/
protected $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
public function addProducts($numpp)
{
$this->products[$numpp] = new Product($numpp, $this);
}
/**
* Product
*
* @ORM\Table(name="Products")
*/
class Product
{
/**
* @var string
*
* @ORM\Column(name="Numpp", type="string", length=6)
* @ORM\Id
*/
private $numpp;
/**
* @var int
*
* @ORM\ManyToMany(targetEntity="Platform", inversedBy="products")
* @ORM\JoinColumn(name="Platform_Id", referencedColumnName="Platform_Id")
* @ORM\Id
*/
private $platforms;
public function __construct($numpp, Platform $platform)
{
$this->numpp = $numpp;
$this->platforms = new ArrayCollection();
$this->platforms[] = $platform;
}
产品实体:
/**
* Platform
*
* @ORM\Table(name="Platforms")
*/
class Platform
{
/**
* @var int
*
* @ORM\Column(name="Platform_Id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Platform_Name", type="string", length=64)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="Product", mappedBy="platforms", cascade={"ALL"}, indexBy="numpp")
*/
protected $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
public function addProducts($numpp)
{
$this->products[$numpp] = new Product($numpp, $this);
}
/**
* Product
*
* @ORM\Table(name="Products")
*/
class Product
{
/**
* @var string
*
* @ORM\Column(name="Numpp", type="string", length=6)
* @ORM\Id
*/
private $numpp;
/**
* @var int
*
* @ORM\ManyToMany(targetEntity="Platform", inversedBy="products")
* @ORM\JoinColumn(name="Platform_Id", referencedColumnName="Platform_Id")
* @ORM\Id
*/
private $platforms;
public function __construct($numpp, Platform $platform)
{
$this->numpp = $numpp;
$this->platforms = new ArrayCollection();
$this->platforms[] = $platform;
}
在我的控制器中尝试创建新产品实体时
$em = $this->getDoctrine()->getManager();
$platform = $em->getRepository("AGAAnalyticsBundle:Platform")->find(1);
$product = new Product('05062', $platform);
$em->persist($product);
$em->flush();
我收到一个错误-无法将值NULL插入到表“dbo.Products”的“Platform\u Id”列中。
和其他使用addProduct方法的方法
$em = $this->getDoctrine()->getManager();
$platform = $em->getRepository("AGAAnalyticsBundle:Platform")->find(1);
$platform->addProduct('05062');
$em->flush();
我收到一个错误-列id必须映射到类AGA\AnalyticsBundle\Entity\Platform中的字段,因为它被另一个类的联接列引用。
请帮助理解我错在哪里,以及我应该如何在我的实体之间正确建立这种关系。如果平台和生产之间存在多对多关系,则不需要在产品表中显示复合键,取而代之的是一个新的表,它将成为平台和产品的连接表。我想这可能会有所帮助,