获取相关对象数据

我有两个实体,Classe和学生,该班级与Student具有OneToMany关系,因为一个班级可以有很多学生。我正在创建一个页面,其中显示与所选班级相关的所有学生的姓名,而我无法访问Classe中学生的数据。实体类


<?php


namespace App\Entity;


use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\Common\Collections\Collection;

use Doctrine\ORM\Mapping as ORM;


/**

 * @ORM\Entity(repositoryClass="App\Repository\ClasseRepository")

 */

class Classe

{

    /**

     * @ORM\Id()

     * @ORM\GeneratedValue()

     * @ORM\Column(type="integer")

     */

    private $id;


    /**

     * @ORM\Column(type="string", length=200)

     */

    private $label;


    /**

     * @ORM\OneToMany(targetEntity="App\Entity\Student", mappedBy="classe", orphanRemoval=true)

     */

    private $Students;


    /**

     * @ORM\OneToMany(targetEntity="App\Entity\WorkDays", mappedBy="class")

     */

    private $schedule;


    public function __construct()

    {

        $this->Students = new ArrayCollection();

        $this->schedule = new ArrayCollection();

    }


    public function getId(): ?int

    {

        return $this->id;

    }


    public function getLabel(): ?string

    {

        return $this->label;

    }


    public function setLabel(string $label): self

    {

        $this->label = $label;


        return $this;

    }


    /**

     * @return Collection|Student[]

     */

    public function getStudents(): Collection

    {

        return $this->Students;

    }


    public function addStudent(Student $student): self

    {

        if (!$this->Students->contains($student)) {

            $this->Students[] = $student;

            $student->setClasse($this);

        }


        return $this;

    }


    public function removeStudent(Student $student): self

    {

        if ($this->Students->contains($student)) {

            $this->Students->removeElement($student);

            // set the owning side to null (unless already changed)

            if ($student->getClasse() === $this) {

                $student->setClasse(null);

            }

        }


        return $this;

    }


就像我之前说过的,一个班级可以有很多学生,我需要获取与该班级相关的学生的姓名并将其放在列表中。

  

倚天杖
浏览 161回答 1
1回答

慕莱坞森

因为实体彼此相关,所以您可以通过Entity Classe或反过来访问类的学生信息。例如:控制器:namespace App\Controller;use Symfony\Component\Routing\Annotation\Route;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;class ClasseController extends AbstractController {&nbsp; /**&nbsp; &nbsp;* @Route("/{id}", name="index", methods={"GET"})&nbsp; &nbsp;*/&nbsp; public function index(ClasseRepository $repository, $id) {&nbsp; &nbsp; &nbsp;return $this->render('index.html.twig', [&nbsp; &nbsp; &nbsp; 'students' => $repository->getStudents($id),&nbsp; &nbsp; ]);}资料库:namespace App\Repository;use App\Entity\Classe;use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;use Symfony\Bridge\Doctrine\RegistryInterface;class ClasseRepository extends ServiceEntityRepository{&nbsp; &nbsp; public function __construct(RegistryInterface $registry)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; parent::__construct($registry, Classe::class);&nbsp; &nbsp; }&nbsp; &nbsp; public function getStudents($id) {&nbsp; &nbsp; &nbsp; return $this->createQueryBuilder('c')&nbsp; &nbsp; &nbsp; &nbsp; ->select('c.Students')&nbsp; &nbsp; &nbsp; &nbsp; ->andWhere('c.id = :id')&nbsp; &nbsp; &nbsp; &nbsp; ->setParameter('id', $id)&nbsp; &nbsp; &nbsp; &nbsp; ->getQuery()&nbsp; &nbsp; &nbsp; &nbsp; ->getResult()&nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP