Php 学说中的关联映射
我有两个表:Php 学说中的关联映射,php,codeigniter,doctrine-orm,Php,Codeigniter,Doctrine Orm,我有两个表:作业和类别。第一个表有一个名为cat\u id的字段,它是对类别的引用。在我的实体类Job中,我有如下注释: /** * @ManyToOne(targetEntity="Category") * @JoinColumn(name="cat_id", referencedColumnName="id") **/ private $category; public function __construct() { $this->category = new \D
作业
和类别
。第一个表有一个名为cat\u id
的字段,它是对类别的引用。在我的实体类Job
中,我有如下注释:
/**
* @ManyToOne(targetEntity="Category")
* @JoinColumn(name="cat_id", referencedColumnName="id")
**/
private $category;
public function __construct()
{
$this->category = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @var Category
*
* @ManyToOne(targetEntity="Category", inversedBy="jobs")
* @JoinColumn(name="cat_id", referencedColumnName="id")
**/
private $category;
public function __construct()
{
// $category would be a single instance of Category,
// not a collection. Otherwise you'd be looking at a
// ManyToMany relationship.
}
/**
* @var ArrayCollection
*
* @OneToMany(targetEntity="Job", mappedBy="category")
*/
private $jobs;
public function __construct()
{
$this->jobs = new \Doctrine\Common\Collections\ArrayCollection();
}
在我的分类课上,我有:
/**
* @OneToMany(targetEntity="Job", mappedBy="job")
* @JoinColumn(name="id", referencedColumnName="cat_id")
*/
private $jobs;
public function __construct()
{
$this->jobs = new \Doctrine\Common\Collections\ArrayCollection();
}
我只想得到所有的工作和他们的类别和所有的工作类别。但我对条令还是比较陌生。您似乎忽略了条令关系映射的“拥有”与“反向”的某些元素。我建议你阅读条令手册以了解更多细节
从本质上讲,1:N关系的一方是拥有方,另一方是相反方。拥有方是实际映射关系的一方,而相反的一方只是反映了这种映射。-在您的代码中,您将JoinColumn
放在了两侧,就好像两个都应该是拥有方一样
您的代码应该将Job.category
属性作为拥有方,将category.jobs
属性作为相反方。因此,首先更改作业实体,使其看起来更像:
/**
* @ManyToOne(targetEntity="Category")
* @JoinColumn(name="cat_id", referencedColumnName="id")
**/
private $category;
public function __construct()
{
$this->category = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @var Category
*
* @ManyToOne(targetEntity="Category", inversedBy="jobs")
* @JoinColumn(name="cat_id", referencedColumnName="id")
**/
private $category;
public function __construct()
{
// $category would be a single instance of Category,
// not a collection. Otherwise you'd be looking at a
// ManyToMany relationship.
}
/**
* @var ArrayCollection
*
* @OneToMany(targetEntity="Job", mappedBy="category")
*/
private $jobs;
public function __construct()
{
$this->jobs = new \Doctrine\Common\Collections\ArrayCollection();
}
然后将类别实体更改为如下所示:
/**
* @ManyToOne(targetEntity="Category")
* @JoinColumn(name="cat_id", referencedColumnName="id")
**/
private $category;
public function __construct()
{
$this->category = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @var Category
*
* @ManyToOne(targetEntity="Category", inversedBy="jobs")
* @JoinColumn(name="cat_id", referencedColumnName="id")
**/
private $category;
public function __construct()
{
// $category would be a single instance of Category,
// not a collection. Otherwise you'd be looking at a
// ManyToMany relationship.
}
/**
* @var ArrayCollection
*
* @OneToMany(targetEntity="Job", mappedBy="category")
*/
private $jobs;
public function __construct()
{
$this->jobs = new \Doctrine\Common\Collections\ArrayCollection();
}
请注意,在Job实体中,我已将inversedBy
属性添加到ManyToOne
注释中,指示Category.jobs
属性作为映射的反方向。然后我从Category.jobs
属性中删除了JoinColumn
,因为反向端实际上不应该直接指定映射;它反映了拥有方的映射。您的实体在这里是正确的,不是吗?我想您应该使用EntityReportory中的查询生成器来创建自定义请求。非常感谢。我查阅了几次文档,试图弄清楚什么是owning
或inverse
side意思。现在一切都按我的要求进行了。:)