您不能查询@MappedSuperClass
. 这在第6.1章的 Doctrine2 文档中也有提及。映射超类:
映射的超类不能是实体,它不是可查询的和持久的
这意味着您必须将目标实体更改为可查询的内容,或者您必须对MachineHasPart
实体进行更改并更改为单表继承。
当我查看您的数据库结构时,我建议将您的Machine
实体更改为具有三个独立的部件关系。一个用于皮带,一个用于气缸,一个用于齿轮。
getParts
然后,您将拥有三个方法getBelts
,而不是泛型,getCylinders
并且getGears
。
如果这真的不是你想要的,那么你可以发表评论。
更新
您也可以通过类继承来解决它。首先创建一个Part
也是实体的基类并在其他类中使用它Belt
,Cylinder
并且Gear
:
部分:
<?php
namespace Machine\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Part
*
* @ORM\Entity
* @ORM\Table("part")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator", type="string")
* @ORM\DiscriminatorMap({
* "part" = "Part",
* "gear" = "Gear",
* "cylinder" = "Cylinder",
* "belt" = "Belt",
* })
* @property int $id
*/
class Part
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var Machine
* @ORM\ManyToOne(targetEntity="Machine\Entity\Machine", inversedBy="parts")
* @ORM\JoinColumn(name="machine_id", referencedColumnName="id", nullable=true)
*/
protected $machine;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* @param int $id
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
//... add setters and getters for machine as normal ...
}
在你的其他部分扩展这个类:
腰带:
<?php
namespace Machine\Entity;
/**
* Belt
*
* @ORM\Entity
*/
class Belt extends Part
{
}
圆柱:
<?php
namespace Machine\Entity;
/**
* Cylinder
*
* @ORM\Entity
*/
class Cylinder extends Part
{
}
齿轮:
<?php
namespace Machine\Entity;
/**
* Gear
*
* @ORM\Entity
*/
class Gear extends Part
{
}
现在在您的机器中涉及如下部件。
机器:
<?php
namespace Machine\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Machine
*
* @ORM\Entity
* @ORM\Table("machine")
* @property int $id
*/
class Machine
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* @param int $id
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @var Collection
* @ORM\OneToMany(targetEntity="Machine\Entity\Part", mappedBy="machine")
*/
protected $parts;
public function __constuct()
{
$parts = new ArrayCollection();
}
/**
*
* @return Collection
*/
public function getParts()
{
return $this->parts;
}
//... add setters and getters for parts as normal ...
}
在你的其他部分扩展这个类: