Symfony3 - 在控制器中创建两个表的连接查询

时间:2017-07-20 03:53:24

标签: php symfony symfony-3.3

如何从我的控制器显示两个表数据。

这是我的控制器代码。

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只显示一个表格数据时。 请帮忙

5 个答案:

答案 0 :(得分:1)

请检查下面提到的解决方案。

$query = $em
       ->createQueryBuilder('t.*,s.*')
       ->from('AppBundle:Teacher','t')
       ->Join('AppBundle:Student','s')
       ->where('t.id=id and s.tid=tid')
       ->getQuery()
       ->getResult();
    }

如果不起作用,请告诉我。

答案 1 :(得分:1)

我假设您已在实体中定义了TeacherStudent之间的关系。在这种情况下,您可以通过调用Student来获取$teacher->getStudents()个对象(假设您已在Teacher实体类中定义了这样的方法)。见Doctrine documentation about association mapping

一对多关系的示例:

<?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;
    // ...
}

QueryBuilder对象中,您可以通过添加以下内容来避免对$teacher->getStudents()次来电的其他查询:

$query = $em
   ->createQueryBuilder('t')
   ->from('AppBundle:Teacher','t')
   ->join('AppBundle:Student','s')
   ->select(array('t', 's'))
   ->where('t.id=id and s.tid=tid')
   ->getQuery()
   ->getResult();
}

如果您实体中的TeacherStudent之间定义了关系,您甚至可以简化联接:

$query = $em
   ->createQueryBuilder('t')
   ->from('AppBundle:Teacher','t')
   ->join('t.students', 's')
   ->select(array('t', 's'))
   ->getQuery()
   ->getResult();
}

如果您通过from()对象创建QueryBuilder对象,则无需调用TeacherRepository方法:

$query = $teacher
   ->createQueryBuilder('t')
   ->join('t.students', 's')
   ->select(array('t', 's'))
   ->getQuery()
   ->getResult();
}

答案 2 :(得分:1)

$query = $em
    ->createQueryBuilder('t')
    ->add('select', 't,s')
    ->from('AppBundle:Teacher', 't')
    ->Join('AppBundle:Student', 's')
    ->where('t.id = s.tid')
    ->getQuery()
    ->getResult();

它完美无缺。

答案 3 :(得分:0)

首先我们从Teachers表中选择所有,然后加入学生。假设您在Teachers模型中的关系名称是学生。在存储库文件中:

public function getWithStudents() {
    return $this->createQueryBuilder('t') 
        ->Join('t.student', 's') 
        ->addSelect('s')
        ->getQuery()->getArrayResult();
}

然后在控制器中调用它:

$teachersWithStudents = $teacher->getWithStudents();

或者在这种情况下你可以

$teachersWithStudents = $teacher->getStudents();

答案 4 :(得分:0)

假设您有两个table.comment表和文章表,并且您想要获取每篇文章的评论

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