我有三节课:
项目类型
/**
* @OneToMany(targetEntity="ProjectTypePhase", mappedBy="project_type")
*/
private $projectTypePhases;
public function __construct()
{
$this->projectTypePhases = new ArrayCollection();
}
/**
* @return mixed
*/
public function getProjectTypePhases()
{
return $this->projectTypePhases;
}
阶段
/**
* @OneToMany(targetEntity="ProjectTypePhase", mappedBy="phase")
*/
private $projectTypePhases;
public function __construct()
{
$this->projectTypePhases = new ArrayCollection();
}
/**
* @return mixed
*/
public function getProjectTypePhases()
{
return $this->projectTypePhases;
}
ProjectTypePhase
/**
* @ManyToOne(targetEntity="ProjectType", inversedBy="project_type_phase")
* @JoinColumn(name="project_type_id", referencedColumnName="id")
*/
private $projectType;
/**
* @ManyToOne(targetEntity="Phase", inversedBy="project_type_phase")
* @JoinColumn(name="phase_id", referencedColumnName="id")
*/
private $phase;
public function __construct($projectType, $phase)
{
$this->projectType = $projectType;
$this->phase = $phase;
}
我通过MySQL工作台填充数据库,因为它们只是id条目(正确吗?)。无论如何,每当我尝试执行$ projectType-> getProjectTypePhases();`它返回一个空集合。当我尝试这个时:
$repository = $this->getDoctrine()->getRepository('AppBundle:ProjectTypePhase');
$projectPhases = $repository->findAll();
我得到了所有条目,但不知何故,项目类型和阶段实体的变量名称为null,即使它们已在数据库中填充。相应的键是正确的,名称已填写。出了什么问题?是否有一些方法需要做我想念的事情?我想要完成的是:
我有一对多关系,它在ProjectType和Phase之间运行良好。但是,由于该表没有id,所以事情没有达到我想要的顺序。我搜索了一个解决方案,这是一个单独的实体,可以处理ProjectType和Phase之间的关系并添加其他列。这是正确的方法吗?
答案 0 :(得分:0)
按如下方式修改ProjectTypePhase
/**
* @ManyToOne(targetEntity="ProjectType", inversedBy="projectTypePhases")
* @JoinColumn(name="project_type_id", referencedColumnName="id")
*/
private $project_type;
/**
* @ManyToOne(targetEntity="Phase", inversedBy="projectTypePhases")
* @JoinColumn(name="phase_id", referencedColumnName="id")
*/
private $phase;
您必须使名称匹配才能使事情有效。然而,教条并没有警告你,这很奇怪