获取相关对象数据



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

<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
 * @ORMEntity(repositoryClass="AppRepositoryClasseRepository")
 */
class Classe
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;
    /**
     * @ORMColumn(type="string", length=200)
     */
    private $label;
    /**
     * @ORMOneToMany(targetEntity="AppEntityStudent", mappedBy="classe", orphanRemoval=true)
     */
    private $Students;
    /**
     * @ORMOneToMany(targetEntity="AppEntityWorkDays", 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;
    }
    /**
     * @return Collection|WorkDays[]
     */
    public function getSchedule(): Collection
    {
        return $this->schedule;
    }
    public function addSchedule(WorkDays $schedule): self
    {
        if (!$this->schedule->contains($schedule)) {
            $this->schedule[] = $schedule;
            $schedule->setClass($this);
        }
        return $this;
    }
    public function removeSchedule(WorkDays $schedule): self
    {
        if ($this->schedule->contains($schedule)) {
            $this->schedule->removeElement($schedule);
            // set the owning side to null (unless already changed)
            if ($schedule->getClass() === $this) {
                $schedule->setClass(null);
            }
        }
        return $this;
    }
}

实体学生

<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
 * @ORMEntity(repositoryClass="AppRepositoryStudentRepository")
 */
class Student
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;
    /**
     * @ORMColumn(type="string", length=200)
     */
    private $firstname;
    /**
     * @ORMColumn(type="string", length=200)
     */
    private $lastname;
    /**
     * @ORMManyToOne(targetEntity="AppEntityclasse", inversedBy="Students")
     * @ORMJoinColumn(nullable=false)
     */
    private $classe;
    public function getId(): ?int
    {
        return $this->id;
    }
    public function getFirstname(): ?string
    {
        return $this->firstname;
    }
    public function setFirstname(string $firstname): self
    {
        $this->firstname = $firstname;
        return $this;
    }
    public function getLastname(): ?string
    {
        return $this->lastname;
    }
    public function setLastname(string $lastname): self
    {
        $this->lastname = $lastname;
        return $this;
    }
    public function getClasse(): ?classe
    {
        return $this->classe;
    }
    public function setClasse(?classe $classe): self
    {
        $this->classe = $classe;
        return $this;
    }
}

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

由于实体彼此相关,因此您可以通过实体类或其他方式访问班级的学生信息。例如:

控制器:

namespace AppController;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyBundleFrameworkBundleControllerAbstractController;
class ClasseController extends AbstractController {
  /**
   * @Route("/{id}", name="index", methods={"GET"})
   */
  public function index(ClasseRepository $repository, $id) {
     return $this->render('index.html.twig', [
      'students' => $repository->getStudents($id),
    ]);
}

存储 库:

namespace AppRepository;
use AppEntityClasse;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use SymfonyBridgeDoctrineRegistryInterface;
class ClasseRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Classe::class);
    }
    public function getStudents($id) {
      return $this->createQueryBuilder('c')
        ->select('c.Students')
        ->andWhere('c.id = :id')
        ->setParameter('id', $id)
        ->getQuery()
        ->getResult()
        ;
    }
}

相关内容

  • 没有找到相关文章

最新更新