如何在原则上使用“外键”? [英] How can I use 'foreign key' on doctrine?
问题描述
我在symfony2和doctrine上创建课程管理系统
I am making the lesson administration system on symfony2 and doctrine
我很难在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;
.
.
}
每个课程都有一个老师在User.php注册。
Each 'Lesson' has one teacher registered in User.php.
如何为此目的编写注释?
How can I write annotation for this purpose?
我还计划每堂课有来自/ Entity / User的多个学生。
我如何为此目的编写注释? (ManyToMany?)
I am also planning that each Lesson has multiple students from /Entity/User. How can I write annotation for this purpose? (ManyToMany?)
我已经研究了,但是我找不到好的文档用于doctrine注释。
I have researched ,but I couldn't find good documents for doctrine annotation.
非常感谢。
推荐答案
这里有一些doctrine注释的备忘单:链接
Here some cheat sheets for doctrine annotations : link
需要在关联的每一侧定义您的变量。
For your problem, you need to define your variables in each side of your associations.
在Lesson.php中:
In 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;
在User.php中:
In User.php :
/**
* @ORM\OneToOne(
* targetEntity="Acme\UserBundle\Entity\Lesson",
* mappedBy="teacher*removethis : name of the variable in lesson.php*"
* )
*/
private $lessons;
是的,ManyToMany适合您的目的:)
And yes, ManyToMany is good for the purpose your are looking for :)
这篇关于如何在原则上使用“外键”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!