我已经阅读了很多其他问题和答案,我的课程似乎看起来还不错,但我仍然得到了'关联名称空间\ Class2#property1指的是拥有方字段Namespace \ Class1#property2,它不存在& #39;错误。
头等舱:
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="projetcs", options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"})
*/
class Project
{
/**
* @var int
* @Id
* @GeneratedValue
* @Column(type="integer", options={"unsigned":true})
*/
protected $id;
/**
* @var ArrayCollection
* @OneToMany(targetEntity="Release", mappedBy="project")
*/
protected $releases;
/**
* Construtor.
*/
public function __construct()
{
$this->releases = new ArrayCollection();
}
/**
* Add release.
*
* @param \Hitec\HDS\Entity\Release $release
*
* @return Project
*/
public function addRelease(\Hitec\HDS\Entity\Release $release)
{
$this->releases[] = $release;
return $this;
}
/**
* Remove release.
*
* @param \Hitec\HDS\Entity\Release $release
*/
public function removeRelease(\Hitec\HDS\Entity\Release $release)
{
$this->releases->removeElement($release);
}
/**
* Get releases.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getReleases()
{
return $this->releases;
}
}
第二课:
/**
* @MappedSuperclass
*/
class Release
{
/**
* @var int
* @Id
* @GeneratedValue
* @Column(type="integer", options={"unsigned":true})
*/
protected $id;
/**
* @var \Hitec\HDS\Entity\Project
* @ManyToOne(targetEntity="Project", inversedBy="releases")
* @JoinColumn(name="project_id", referencedColumnName="id")
*/
protected $project;
/**
* Set project.
*
* @param \Hitec\HDS\Entity\Project $project
*
* @return Release
*/
public function setProject(\Hitec\HDS\Entity\Project $project)
{
$project->addRelease($this);
$this->project = $project;
return $this;
}
/**
* Get project.
*
* @return \Hitec\HDS\Entity\Project
*/
public function getProject()
{
return $this->project;
}
}
运行doctrine orm:validate-schema
时,我得到:
[Mapping] FAIL - 实体类' Hitec \ HDS \ Entity \ Project'映射无效:
这可能与第二类是MappedSuperclass的事实有关吗?
答案 0 :(得分:1)
@MappedSuperclass
不是实体。你不能参考它。你为什么想这样做?
为了使事情有效,你应该用一些@Entity
class Project
{
//... rest of the code
/**
* @var ArrayCollection
* @OneToMany(targetEntity="ConcreteRelease", mappedBy="project")
*/
protected $releases;
//... rest of the code
}
/**
* @Entity
*/
class ConcreteRelease extends Release {
// ... rest of the code
}
或者更改Release
的注释,使其成为@Entity
。
例如:
/**
* @Entity
* @Table(name="release")
*/
class Release {...}
另一种选择是在DB中使用鉴别器字段。见教义doc for details:
class Project
{
//... rest of the code
/**
* @var ArrayCollection
* @OneToMany(targetEntity="AppRelease", mappedBy="project")
*/
protected $releases;
//... rest of the code
}
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"app" = "AppRelease", "package" = "PackageRelease"})
* @Table(name="release")
*/
class AppRelease {...}
/**
* @Entity
*/
class PackageRelease {...}