1

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

这是我的控制器的代码。

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

4

5 回答 5

1

我假设您已经定义了实体之间TeacherStudent实体中的关系。在这种情况下,您可以Student通过调用来获取对象$teacher->getStudents()(假设您已经在Teacher实体类中定义了这样的方法)。请参阅有关关联映射的 Doctrine 文档

一对多关系的示例:

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

如果如上所述在您的实体之间Teacher和实体中定义了关系,您甚至可以简化连接:Student

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

此外,如果您通过对象创建对象,则无需调用该from()方法:QueryBuilderTeacherRepository

$query = $teacher
   ->createQueryBuilder('t')
   ->join('t.students', 's')
   ->select(array('t', 's'))
   ->getQuery()
   ->getResult();
}
于 2017-07-20T06:28:32.730 回答
1

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

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

让我知道它是否不起作用。

于 2017-07-20T04:08:46.220 回答
1
$query = $em
    ->createQueryBuilder('t')
    ->add('select', 't,s')
    ->from('AppBundle:Teacher', 't')
    ->Join('AppBundle:Student', 's')
    ->where('t.id = s.tid')
    ->getQuery()
    ->getResult();

它工作完美。

于 2017-07-21T17:14:56.050 回答
0

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

 $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);
于 2018-02-06T18:00:07.303 回答
0

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

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

然后在控制器中调用它:

$teachersWithStudents = $teacher->getWithStudents();

或者在这种情况下,您可以

$teachersWithStudents = $teacher->getStudents();

于 2017-07-20T08:55:54.207 回答