1 回答
TA贡献1810条经验 获得超4个赞
因为实体彼此相关,所以您可以通过Entity Classe或反过来访问类的学生信息。例如:
控制器:
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()
;
}
}
- 1 回答
- 0 关注
- 160 浏览
添加回答
举报