如何在学说中使用“外键”?

时间:2022-09-15 19:29:13

I am making the lesson administration system on symfony2 and doctrine

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

I am confused to use foreign key in doctrine.

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

/Entity/User.php

/Entity/User.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

/Entity/Lesson.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

非常感谢

1 个解决方案

#1


9  

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对你正在寻找的目的有好处:)

#1


9  

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对你正在寻找的目的有好处:)