问题描述
我有2个实体-平台和产品。在产品表中,我具有按[产品ID +平台ID]的组合主键。一种产品可以在许多平台上出现,因此一个平台可以包含许多产品,因此关联为ManyToMany。
平台实体:
// E2E testing with spectron and jest
const app = new Application({
path: electronPath,args: [appPath]
})
describe('mock Date() in login window',() => {
await app.client.windowByIndex(0)
console.log('app.browserWindow.fakeTimers')
console.log(app.browserWindow.fakeTimers)
// => undefined
})
产品实体:
/**
* 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实体时...
/**
* Product
*
* @ORM\Table(name="Products")
*/
class Product
{
/**
* @var string
*
* @ORM\Column(name="Numpp",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;
}
我收到一个错误-无法将值NULL插入表'dbo.Products'的列'Platform_Id'中
以其他方式使用addProduct方法...
$em = $this->getDoctrine()->getManager();
$platform = $em->getRepository("AGAAnalyticsBundle:Platform")->find(1);
$product = new Product('05062',$platform);
$em->persist($product);
$em->flush();
我收到一个错误-列ID必须映射到类AGA \ AnalyticsBundle \ Entity \ Platform中的字段,因为它是由另一个类的联接列引用的。
请帮助了解我在哪里错了,以及如何正确建立实体之间的这种关系。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)