首页 文章

在zf3中使用doctrine OneToOne与条件的关系

提问于
浏览
0

我想在实体用户中使用Doctrine的关系OneToOne . 例如 . 用户有2门考试 - 第一次和纠正 . 所有考试都在一个表中,但标志为 is_corrective . 对于一个用户,我如何为这两种类型的考试使用OneToOne关系?

1 回答

  • 0

    您可以使用Single Table Inheritance .

    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个关系:

    /**
     * @ORM\OneToOne(targetEntity="FirstExam")
     */
    private $firstExam;
    
    /**
     * @ORM\OneToOne(targetEntity="CorrectiveExam")
     */
    private $correctiveExam;
    

    使用两个实体的相同表,并且“is_corrective”列用作鉴别器 .

相关问题