Search code examples
symfonydoctrine-ormsymfony4

showing only current user's relations


I have 2 entities, User and courses, each student can have many courses and each course can have many students. Anyway, I'm trying to make it so that when a user connects to his account, it shows him all the courses he's related to. I know I have to do it from my controller but how do I get only the ones he's related to. my entities

<?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;
    }


}


Solution

  • Well, because you apparently have a bi-directional relationship (inversedBy and mappedBy) you dont need to do anything special. Doctrine has already done this. Just grab the user in the controller:

    // 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();
    
    

    And iterate as you'd like through their related courses:

    foreach ($user->getCourses() as $course) {
    
    }
    

    Because getCourses also returns a Collection instance, you can also use the ->map and ->filter functions that ship with the Collection class...