【问题标题】:Get related objects data获取相关对象数据
【发布时间】:2019-04-24 21:11:14
【问题描述】:

我有两个实体,Classe 和 Students,班级与 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;
    }

    /**
     * @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 App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\StudentRepository")
 */
class Student
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=200)
     */
    private $firstname;

    /**
     * @ORM\Column(type="string", length=200)
     */
    private $lastname;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\classe", inversedBy="Students")
     * @ORM\JoinColumn(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;
    }
}

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

【问题讨论】:

  • 嗨,欢迎来到 SO。您如何尝试访问数据?您向我们展示了实体,但没有展示您如何使用它们。
  • 你可以通过 $classObj->getStudents() 方法获取所有受尊敬班级的学生:

标签: php symfony doctrine-orm symfony4


【解决方案1】:

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

控制器:

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

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 App\Repository;

use App\Entity\Classe;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

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

}

【讨论】:

  • 抱歉回复晚了,谢谢,这正是我要找的。​​span>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 2018-11-19
  • 2010-11-11
  • 2017-09-12
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多