Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony3-在控制器中创建两个表的联接查询_Php_Symfony_Symfony 3.3 - Fatal编程技术网

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