在zf3中将条件式OneToOne关系与条件一起使用 [英] Using doctrine OneToOne relation with condition in zf3
本文介绍了在zf3中将条件式OneToOne关系与条件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在实体User中使用Doctrine的关系OneToOne. 例如.用户有2项考试-第一次和更正.所有考试都放在一个表格中,但标记为 is_corrective .如何为一个用户的这两种类型的考试使用OneToOne关系?
I want to use Doctrine's relation OneToOne in entity User. Eg. User has 2 exams - first and corrective. All exams are in one table but with flag is_corrective. How can I use OneToOne relation for this two types of exams for one user?
推荐答案
You can use Single Table Inheritance for this.
1)创建实体 FirstExam :
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="exam_table_name")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="is_corrective", type="integer")
* @ORM\DiscriminatorMap({"0" = "FirstExam", "1" = "CorrectiveExam"})
*/
class FirstExam
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
2)和实体 CorrectiveExam :
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class CorrectiveExam extends FirstExam
{
}
3)然后您可以在User实体中定义2个关系:
3)then you can define 2 relations in the User entity:
/**
* @ORM\OneToOne(targetEntity="FirstExam")
*/
private $firstExam;
/**
* @ORM\OneToOne(targetEntity="CorrectiveExam")
*/
private $correctiveExam;
两个实体使用相同的表,并且"is_corrective"列用作区分符.
The same table for both entities is used and "is_corrective" column is used as a discriminator.
这篇关于在zf3中将条件式OneToOne关系与条件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文