我尝试在Doctrine 2中的场景中实现以下内容:
这在Skipper中如下所示:
导出到基于Doctrine 2注释的实体,这看起来像这样(Getter和setter被剪断):
Driver.php
<?php
namespace Racing\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Driver
{
/**
* @ORM\Id
* @ORM\Column(type="bigint")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $Id;
/**
* @ORM\Column(type="text", nullable=false)
*/
private $Name;
}
Vehicle.php
<?php
namespace Racing\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Vehicle
{
/**
* @ORM\Id
* @ORM\Column(type="bigint")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $Id;
/**
* @ORM\Column(type="text", nullable=false)
*/
private $Name;
}
Race.php
<?php
namespace Racing\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Race
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $Id;
/**
* @ORM\Column(type="text", nullable=false)
*/
private $Name;
}
Accident.php
<?php
namespace Racing\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Accident
{
/**
* @ORM\Column(type="text", nullable=false)
*/
private $Description;
/**
* @ORM\ManyToOne(targetEntity="Racing\Entity\RacingVehicle")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="Racing_Vehicle_Driver_Id", referencedColumnName="Driver_Id", nullable=false),
* @ORM\JoinColumn(name="Racing_Vehicle_Vehicle_Id", referencedColumnName="Vehicle_Id", nullable=false)
* })
*/
private $RacingVehicle;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Racing\Entity\Race")
* @ORM\JoinColumn(name="Race_Id", referencedColumnName="Id", nullable=false)
*/
private $Race;
}
RacingVehicle.php
<?php
namespace Racing\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class RacingVehicle
{
/**
* @ORM\Column(type="integer", nullable=false)
*/
private $Startnumber;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Racing\Entity\Driver")
* @ORM\JoinColumn(name="Driver_Id", referencedColumnName="Id", nullable=false)
*/
private $Driver;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Racing\Entity\Vehicle")
* @ORM\JoinColumn(name="Vehicle_Id", referencedColumnName="Id", nullable=false)
*/
private $Vehicle;
}
测试样本如下所示:
// Get the entity manager
$sservicelocator = $this->getServiceLocator();
$entitymanager = $sservicelocator->get('doctrine.entitymanager.orm_default');
// Create a driver
$driver = new Driver();
$driver->setName("Michael Schumacher");
$entitymanager->persist($driver);
// Create a vehicle
$vehicle = new Vehicle();
$vehicle->setName("Ferrari");
$entitymanager->persist($vehicle);
// Create a race
$race = new Race();
$race->setName("Monaco");
$entitymanager->persist($race);
// Create a racing vehicle
$racingvehicle = new RacingVehicle();
$racingvehicle->setDriver($driver);
$racingvehicle->setVehicle($vehicle);
$racingvehicle->setStartnumber(42);
$entitymanager->persist($racingvehicle);
// Create an accident
$accident = new Accident();
$accident->setRace($race);
$accident->setRacingVehicle($racingvehicle);
$entitymanager->persist($accident); // <-------------- Error line
// Flush
$entitymanager->flush();
一旦我试图坚持这次事故,我就会收到这个错误:
The given entity of type 'Racing\Entity\Accident' (Racing\Entity\Accident@0000000072a5fbd400007f1c433b761e) has no identity/no id values set. It cannot be added to the identity map.
所以我的问题:如何创建一个自己的实体(事故),将RacingVehicle和Race作为主键,同时真正拥有自己的PHP类实体(我可以创建多对多的关系,但在那种情况下我赢了&# 39;拥有自己的意外实体。)
答案 0 :(得分:1)
我认为您可以将Driver
和Vehicle
添加为RacingVehicle
的ID。然后Accident
看起来像这样:
<?php
namespace Racing\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Accident
{
//...
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Racing\Entity\Driver")
* @ORM\JoinColumn(name="Driver_Id", referencedColumnName="Id", nullable=false)
*/
private $Driver;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Racing\Entity\Vehicle")
* @ORM\JoinColumn(name="Vehicle_Id", referencedColumnName="Id", nullable=false)
*/
private $Vehicle;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Racing\Entity\Race")
* @ORM\JoinColumn(name="Race_Id", referencedColumnName="Id", nullable=false)
*/
private $Race;
//...
}
这意味着您将获得三列的复合键。您现在可以使用Driver_Id
列和Vehicle_Id
列来解决事故中的RacingVehicle
。
不确定是否可以直接映射它,因为它可能会导致索引冲突(因为Driver_Id
的索引与Driver
表中的标识符和{{1}的索引相关联} Vehicle_Id
表中的标识符)但您可以在生命周期事件中加载或创建Vehicle
的代理。