多对多,一对多或多对一 [英] Many to Many, One to Many or Many to One

查看:84
本文介绍了多对多,一对多或多对一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清Doctrine 2 ORM的关系,我以为我掌握了它,但是在阅读了一些Symfony食谱后,我怀疑我实际上有些困惑.

I am trying to get my head around Doctrine 2 ORM relationships, I thought I had the hang of it but after reading a few symfony cookbook entries, I suspect I am actually a little confused.

我目前有一个系统,其中一个模板可以包含多个模块(包括每种类型的多个模块),并且多个模板可以使用同一模块.

I currently have a system where a template can contain multiple modules (including more than one of each type) and multiple templates can use the same module.

我认为这可以保证实现ManyToMany关系,实际上看着我的桌子,它似乎运行得很好.

I thought that this would warrant a ManyToMany relationship and indeed looking at my table, it seems to work quite well.

但是,当我编写数据库查询时,我意识到我需要按一定顺序加载模块,这意味着我的联接表需要有第三个"order_by"列.我读过一个真正的联接表只有两列.

However I realised as I was writing the database query that I needed the modules to load in a certain order, which means my join table needs to have a third 'order_by' column. I've read that a true join table is only ever two columns.

引起混乱.我应该在实体中设置什么?

Hence the confusion. What should I set this up as in my entities?

推荐答案

就像@Kris说的那样-您将对中间实体进行一对多投票.如果您选择了多对多",那么您将没有中间表的类文件,这在大多数情况下是一个问题.

Like @Kris said - You'll go for One to Many towards middle entity. If you go for Many to Many instead then you won't have class file for middle table which is an issue in the most cases.

M-N假设:一个学生在许多课程中学习,一个课程可以有很多学生.

M-N assumption: ONE Student studies in MANY Courses and ONE COURSE can have MANY Students.

下面的两个示例都在数据库中提供了此ERD,但您想使用一个到许多版本.

Both of the examples below give you this ERD in database but you want to go for ONE to MANY version.

许多到许多:

这将在数据库中创建 StudentCourse 实体,但是您看不到要处理的实际类文件.

This will create StudentCourse entity in database but as you see no actual class file for you to deal with.

class Student
{
    protected $id;
    protected $name;

    /**
     * @ORM\ManyToMany(targetEntity="Course")
     * @ORM\JoinTable(
     * name="StudentCourse",
     * joinColumns={@ORM\JoinColumn(name="studentId", referencedColumnName="id")},
     * inverseJoinColumns={@ORM\JoinColumn(name="courseId", referencedColumnName="id")}
     * )
     */
    private $course;
}

class Course
{
    protected $id;
    protected $name;
}

一对多:

这将在数据库中创建 StudentCourse 实体,如您所见,在编码时,有一个实际的类文件供您处理.persist()等.

This will create StudentCourse entity in database and as you see there is an actual class file for you to deal with when coding e.g. persist() etc.

class Student
{
    protected $id;
    protected $name;

    /**
     * @ORM\OneToMany(targetEntity="StudentCourse", mappedBy="studentMap",
     * cascade={"persist", "remove"})
     */
    protected $studentInverse;
}

class StudentCourse
{
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Course", inversedBy="courseInverse")
     * @ORM\JoinColumn(name="course", referencedColumnName="id",
     * nullable=false, onDelete="CASCADE")
     */
    protected $courseMap;

    /**
     * @ORM\ManyToOne(targetEntity="Student", inversedBy="studentInverse")
     * @ORM\JoinColumn(name="student", referencedColumnName="id",
     * nullable=false, onDelete="CASCADE")
     */
    protected $studentMap;
}

class Course
{
    protected $id;
    protected $name;

    /**
     * @ORM\OneToMany(targetEntity="StudentCourse", mappedBy="courseMap",
     * cascade={"persist", "remove"})
     */
    protected $courseInverse;
}

onDelete ="CASCADE" cascade = {"persist","remove"} 位不是强制性的.他们处理数据冗余.使用冗余关系是否不好?

onDelete="CASCADE" and cascade={"persist", "remove"} bits are not compulsory. They handle data redundancy. Is it bad to use redundant relationships?

这篇关于多对多,一对多或多对一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆