database 我如何在教义上使用“外键”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15970191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:56:14  来源:igfitidea点击:

How can I use 'foreign key' on doctrine?

databasesymfonydoctrine-orm

提问by whitebear

I am making the lesson administration system on symfony2 and doctrine

我正在制作关于 symfony2 和学说的课程管理系统

I am confused to use foreign key in doctrine.

我很困惑在学说中使用外键。

/Entity/User.php

/实体/用户.php

class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     *@ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\Lesson", inversedBy("teacher"))
     */
    protected $id;
    .
    .
}

/Entity/Lesson.php

/实体/课程.php

class Lesson
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     *
     * @ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy("id"))
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $teacher;
    .
    .
}

Each 'Lesson' has one teacher registered in User.php.

每个“课程”都有一位在 User.php 中注册的教师。

How can I write annotation for this purpose?

我如何为此目的编写注释?

I am also planning that each Lesson has multiple students from /Entity/User. How can I write annotation for this purpose? (ManyToMany?)

我还计划每个课程都有来自 /Entity/User 的多个学生。我如何为此目的编写注释?(多对多?)

I have researched ,but I couldn't find good documents for doctrine annotation.

我已经研究过,但找不到用于学说注释的好文件。

thanks a lot

多谢

采纳答案by Pierrickouw

Here some cheat sheets for doctrine annotations : link

这里有一些用于学说注释的备忘单:链接

For your problem, you need to define your variables in each side of your associations.

对于您的问题,您需要在关联的每一侧定义变量。

In Lesson.php :

在 Lesson.php 中:

/**
 * @ORM\OneToOne(
 *     targetEntity="Acme\UserBundle\Entity\User", 
 *     inversedBy="lessons*removethis : name of the variable in user.php*"
 * )
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $teacher;

In User.php :

在 User.php 中:

/**
 * @ORM\OneToOne(
 *     targetEntity="Acme\UserBundle\Entity\Lesson", 
 *     mappedBy="teacher*removethis : name of the variable in lesson.php*"
 * )
 */
private $lessons;

And yes, ManyToMany is good for the purpose your are looking for :)

是的,ManyToMany 非常适合您正在寻找的目的:)