我对 Doctrine、继承和映射有疑问。这是一款基于城市管理的网页游戏。我不会复制所有的代码,不是因为它是秘密的,而是因为我已经有太多的东西可以放在这里了。
一般设计如下:
每个城镇都有多个结构都比较相似,然后我决定做一个 MappedSuperClass 如下
/**
* @ORM\MappedSuperclass
*/
class StructuresSuperClass{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="BuildingType")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity="Town", cascade={"persist"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $town;
在结构中有路线,建筑物,在建筑物中有一个特殊的 TownCenter 有它自己的实体,它们如下所示:
路线:
use Spagi\GameBundle\Entity\StructuresSuperClass as SSC;
/**
* @ORM\Entity
* @ORM\Table(name="Route")
*/
class Route extends SSC{
/**
* @ORM\OneToMany(targetEntity="RouteOrders", mappedBy="route", cascade={"all"}, orphanRemoval=true)
*/
protected $orders;
/**
* @ORM\ManyToOne(targetEntity="Building", cascade={"persist", "remove"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
protected $origin;
/**
* @ORM\ManyToOne(targetEntity="Building", cascade={"persist", "remove"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
protected $destination;
建筑物:
use Spagi\GameBundle\Entity\StructuresSuperClass as SSC;
/**
* @ORM\Entity
* @ORM\Table(name="Building")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"TC" = "TownCenter", "Building" = "Building"})
*/
class Building extends SSC{
/**
* @ORM\OneToOne(targetEntity="Inventory", cascade={"persist", "remove"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $inventory;
/**
* @ORM\Column(type="integer")
*/
private $radius;
/**
* @ORM\Column(type="integer")
*/
private $theta;
和市中心:
use Spagi\GameBundle\Entity\Building as Buildings;
/**
* @ORM\Entity
*/
class TownCenter extends Buildings{
public function __construct(){
$this->setRadius(0);
$this->setTheta(0);
parent::__construct();
}
在城镇实体中,我需要分别访问其中的每一个,所以我创建了 3 个 OneToMany
/**
* @ORM\Entity
* @ORM\Table(name="Town")
*/
class Town{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/** SNIP **/
/**
* @ORM\OneToMany(targetEntity="Building", mappedBy="town", cascade={"all"}, orphanRemoval=true)
*/
protected $buildings;
/**
* @ORM\OneToMany(targetEntity="TownCenter", mappedBy="town", cascade={"all"}, orphanRemoval=true)
*/
protected $townCenter;
/**
* @ORM\OneToMany(targetEntity="Route", mappedBy="town", cascade={"all"}, orphanRemoval=true)
*/
protected $routes;
问题是我总是收到一条教义警告我的实体无效,如下所示
The field Spagi\GameBundle\Entity\Town#buildings is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity Spagi\GameBundle\Entity\Building#town does not contain the required 'inversedBy=buildings' attribute.
The field Spagi\GameBundle\Entity\Town#townCenter is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity Spagi\GameBundle\Entity\TownCenter#town does not contain the required 'inversedBy=townCenter' attribute.
The field Spagi\GameBundle\Entity\Town#routes is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity Spagi\GameBundle\Entity\Route#town does not contain the required 'inversedBy=routes' attribute.
据我了解,随着时间的推移和数据库的增长,这将导致性能下降。这个关系 Building#town、TownCenter#town 和 Route#town 都继承自同一个文件。我不能写 inversedBy={'buildings','townCenter','routes'} (好吧,我没试过,但我很怀疑)。
但是,我想知道除了在每个实体中定义 $town 之外是否有解决此问题的方法,从而错过了继承这一点。