仅显示当前用户的关系
showing only current user's relations
我有 2 个实体,用户和课程,每个学生可以有很多课程,每个课程可以有很多学生。无论如何,我正在努力做到这一点,以便当用户连接到他的帐户时,它会向他显示与他相关的所有课程。我知道我必须通过我的控制器来完成,但我如何才能只获得与他相关的控制器。
我的实体
<?php
/**
* @ORM\Entity
*/
class User implements UserInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
public function getId()
{
return $this->id;
}
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Courses", mappedBy="Users")
*/
private $courses;
public function __construct()
{
$this->courses = new ArrayCollection();
}
/**
* @return Collection|course[]
*/
public function getCourses(): Collection
{
return $this->courses;
}
public function addCourse(Course $course): self
{
if (!$this->courses->contains($course)) {
$this->courses[] = $course;
$course->addUser($this);
return $this;
}
return $this;
}
public function removeCourse(Course $course): self
{
if ($this->courses->contains($course)) {
$this->courses->removeElement($course);
$course->removeUser($this);
}
return $this;
}
}
<?php
/**
* @ORM\Entity(repositoryClass="App\Repository\CourseRepository")
*/
class Course
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=55)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\user", inversedBy="courses")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getname(): ?string
{
return $this->name;
}
public function setname(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|user[]
*/
public function getUser(): Collection
{
return $this->user;
}
public function addUser(user $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
}
return $this;
}
public function removeUser(user $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
}
return $this;
}
}
好吧,因为您显然具有双向关系(inversedBy 和 mappedBy),所以您不需要做任何特殊的事情。 Doctrine 已经做到了这一点。只需在控制器中抓取用户:
// Once you have a protected controller route,
// you can use $this->getUser() from inside the
// controller action to access the current authenticated user.
$user = $this->getUser();
并根据您的喜好迭代他们的相关课程:
foreach ($user->getCourses() as $course) {
}
因为 getCourses
也是 returns 一个 Collection
实例,您还可以使用集合 [=22] 附带的 ->map
和 ->filter
函数=]...
我有 2 个实体,用户和课程,每个学生可以有很多课程,每个课程可以有很多学生。无论如何,我正在努力做到这一点,以便当用户连接到他的帐户时,它会向他显示与他相关的所有课程。我知道我必须通过我的控制器来完成,但我如何才能只获得与他相关的控制器。 我的实体
<?php
/**
* @ORM\Entity
*/
class User implements UserInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
public function getId()
{
return $this->id;
}
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Courses", mappedBy="Users")
*/
private $courses;
public function __construct()
{
$this->courses = new ArrayCollection();
}
/**
* @return Collection|course[]
*/
public function getCourses(): Collection
{
return $this->courses;
}
public function addCourse(Course $course): self
{
if (!$this->courses->contains($course)) {
$this->courses[] = $course;
$course->addUser($this);
return $this;
}
return $this;
}
public function removeCourse(Course $course): self
{
if ($this->courses->contains($course)) {
$this->courses->removeElement($course);
$course->removeUser($this);
}
return $this;
}
}
<?php
/**
* @ORM\Entity(repositoryClass="App\Repository\CourseRepository")
*/
class Course
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=55)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\user", inversedBy="courses")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getname(): ?string
{
return $this->name;
}
public function setname(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|user[]
*/
public function getUser(): Collection
{
return $this->user;
}
public function addUser(user $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
}
return $this;
}
public function removeUser(user $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
}
return $this;
}
}
好吧,因为您显然具有双向关系(inversedBy 和 mappedBy),所以您不需要做任何特殊的事情。 Doctrine 已经做到了这一点。只需在控制器中抓取用户:
// Once you have a protected controller route,
// you can use $this->getUser() from inside the
// controller action to access the current authenticated user.
$user = $this->getUser();
并根据您的喜好迭代他们的相关课程:
foreach ($user->getCourses() as $course) {
}
因为 getCourses
也是 returns 一个 Collection
实例,您还可以使用集合 [=22] 附带的 ->map
和 ->filter
函数=]...