I have an entity that in it's lifetime moves to different tables. Like so:
A village has many units An army movement on the map contains many units After an attack, the log file contains many units (with extra amount_killed
column)
So anyway, this Unit
is what I consider my entity, that can either be attached to a Village, Movement or Log via a table and foreign key.
I would think in Doctrine there is a way to say, this is an entity and it can as a foreign key to multiple tables. The thing is, I want to pluck Units from my Village and put them into a Movement but they're not defined as the same item so it will break.
My code for current usage will look something like this for transferring an entity to another table:
$villageUnits = $village->getVillageUnits();
$movementUnits = new ArrayCollection();
foreach ($villageUnits as $villageUnit) {
$movementUnit = new MovementUnit();
$movementUnit
->setLevel($villageUnit->getLevel());
->setAmount($villageUnit->getAmount());
->setUnitType($villageUnit->getUnitType());
$movementUnits->add($movementUnit);
}
At the moment, they're all seperate entities defined with Doctrine as follows:
TABLES
village_units
---------
id, village_id, unit_type_id, amount, level
movement_units
---------
id, movement_id, unit_type_id, amount, level
log_units
---------
id, log_id, unit_type_id, amount, amount_killed, level
ENTITIES
/**
* MovementUnit
*
* @ORM\Table(name="movement_units")
* @ORM\Entity
*/
class MovementUnit
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\Id
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="amount", type="integer", nullable=false)
*/
private $amount;
/**
* @var integer
*
* @ORM\Column(name="level", type="integer", nullable=false)
*/
private $level;
/**
* @var Movement
*
* @ORM\ManyToOne(targetEntity="Movement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="movement_id", referencedColumnName="id")
* })
*/
private $movement;
/**
* @var UnitType
*
* @ORM\OneToOne(targetEntity="UnitType")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="unit_type_id", referencedColumnName="id")
* })
*/
private $unitType;
}
/**
* VillageUnit
*
* @ORM\Table(name="village_units")
* @ORM\Entity
*/
class VillageUnit
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\Id
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="amount", type="integer", nullable=false)
*/
private $amount;
/**
* @var integer
*
* @ORM\Column(name="level", type="integer", nullable=false)
*/
private $level;
/**
* @var Village
*
* @ORM\ManyToOne(targetEntity="Village")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="village_id", referencedColumnName="id")
* })
*/
private $village;
/**
* @var UnitType
*
* @ORM\OneToOne(targetEntity="UnitType")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="unit_type_id", referencedColumnName="id")
* })
*/
private $unitType;
}
/**
* LogUnit
*
* @ORM\Table(name="log_units")
* @ORM\Entity
*/
class LogUnit
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\Id
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="amount", type="integer", nullable=false)
*/
private $amount;
/**
* @var integer
*
* @ORM\Column(name="dead", type="integer", nullable=false)
*/
private $dead;
/**
* @var integer
*
* @ORM\Column(name="level", type="integer", nullable=false)
*/
private $level;
/**
* @var LogArmy
*
* @ORM\ManyToOne(targetEntity="LogArmy")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="log_army_id", referencedColumnName="id")
* })
*/
private $logArmy;
/**
* @var UnitType
*
* @ORM\OneToOne(targetEntity="UnitType")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="unit_type_id", referencedColumnName="id")
* })
*/
private $unitType;
}
I had the exact same problem and solved it with the answer of Robin in this question: Doctrine 2 Inheritance Mapping with Association
Just change the inheritanceType from "SINGLE_TABLE" to "JOINED" and your setup is working. If not, try using doctrine-module orm:schema-tool:create it will create the tables for you.