Php Symfony3-在控制器中创建两个表的联接查询
如何显示来自控制器的两个表数据 这是我的控制器代码Php Symfony3-在控制器中创建两个表的联接查询,php,symfony,symfony-3.3,Php,Symfony,Symfony 3.3,如何显示来自控制器的两个表数据 这是我的控制器代码 class TestController extends Controller { public function showAction(Request $request) { $em = $this->getDoctrine()->getManager(); $teacher = $this->getDoctrine()->getRepository(Teacher::cla
class TestController extends Controller
{
public function showAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$teacher = $this->getDoctrine()->getRepository(Teacher::class);
$query = $em
->createQueryBuilder('t')
->from('AppBundle:Teacher','t')
->Join('AppBundle:Student','s')
->where('t.id=id and s.tid=tid')
->getQuery()
->getResult();
}
}
当print\r
时,它只显示一个表数据。
请帮助请检查下面提到的解决方案
$query = $em
->createQueryBuilder('t.*,s.*')
->from('AppBundle:Teacher','t')
->Join('AppBundle:Student','s')
->where('t.id=id and s.tid=tid')
->getQuery()
->getResult();
}
如果不起作用,请告诉我。我假设您已在实体中定义了
教师
和学生
之间的关系。在这种情况下,您可以通过调用$teacher->getStudents()
(假设您已经在teacher
实体类中定义了这样一个方法)来获取Student
对象。看
一对多关系的示例:
<?php
use Doctrine\Common\Collections\ArrayCollection;
/** @Entity */
class Teacher
{
// ...
/**
* One Teacher has Many Students.
* @OneToMany(targetEntity="Student", mappedBy="teacher")
*/
private $students;
// ...
public function __construct() {
$this->students = new ArrayCollection();
}
}
/** @Entity */
class Student
{
// ...
/**
* Many Students have One Teacher.
* @ManyToOne(targetEntity="Teacher", inversedBy="students")
* @JoinColumn(name="teacher_id", referencedColumnName="id")
*/
private $teacher;
// ...
}
如果上述实体中的教师
和学生
之间定义了关系,您甚至可以简化联接:
$query = $em
->createQueryBuilder('t')
->from('AppBundle:Teacher','t')
->join('t.students', 's')
->select(array('t', 's'))
->getQuery()
->getResult();
}
此外,如果通过TeacherRepository
对象创建QueryBuilder
对象,则无需调用from()
方法:
$query = $teacher
->createQueryBuilder('t')
->join('t.students', 's')
->select(array('t', 's'))
->getQuery()
->getResult();
}
首先,我们从教师表中选择所有,然后加入学生。假设您在教师模型中的关系名称为学生。在存储库文件中:
public function getWithStudents() {
return $this->createQueryBuilder('t')
->Join('t.student', 's')
->addSelect('s')
->getQuery()->getArrayResult();
}
然后在控制器中调用它:
$teachersWithStudents=$teacher->getWithStudents()代码>
或者在这种情况下,你可以
$teachersWithStudents=$teacher->getStudents()代码>
它工作得很好。假设您有两个表。comment table和article table,并且希望获取每篇文章的注释
$commentContent = $em
// automatically knows to select Comment
// the "c" is an alias you'll use in the rest of the query
->createQueryBuilder('c')
->select('c.message, c.name')////Fields required to display
->from('AppBundle:Comment','c')
->join('AppBundle:Article','a')
->where('c.idArticle=a.id and c.publish_mainPage = 1')
->orderBy('c.idArticle', 'DESC')
->getQuery()
->getResult();
var_dump($commentContent);
我认为这不会像预期的那样起作用,因为createQueryBuilder()
方法的第一个参数只是定义了应该填充的实体的别名。请参阅下面我的答案以了解可能的解决方案。
$query = $em
->createQueryBuilder('t')
->add('select', 't,s')
->from('AppBundle:Teacher', 't')
->Join('AppBundle:Student', 's')
->where('t.id = s.tid')
->getQuery()
->getResult();
$commentContent = $em
// automatically knows to select Comment
// the "c" is an alias you'll use in the rest of the query
->createQueryBuilder('c')
->select('c.message, c.name')////Fields required to display
->from('AppBundle:Comment','c')
->join('AppBundle:Article','a')
->where('c.idArticle=a.id and c.publish_mainPage = 1')
->orderBy('c.idArticle', 'DESC')
->getQuery()
->getResult();
var_dump($commentContent);