2017-07-20 62 views
1

如何從控制器顯示兩個表數據。Symfony3 - 在控制器中創建兩個表的聯接查詢

這是我的控制器代碼。

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它只顯示一個表數據。 請幫忙

回答

1

請檢查下面提到的解決方案。

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

讓我知道它是否無效。

+0

取意見,我不認爲像預期的那樣的'createQueryBuilder的第一個參數,這將工作( )'方法只是定義了應該填充的實體的別名。有關可能的解決方案,請參閱以下答案。 – W0rma

1

我假設您已在您的實體中定義了TeacherStudent之間的關係。在這種情況下,您可以通過調用$teacher->getStudents()來獲取Student對象(假設您已在Teacher實體類中定義了這種方法)。見Doctrine documentation about association mapping

例如,對於一個一對多的關係:

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

<?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()調用額外查詢的需要

如果您的實體中的TeacherStudent之間存在上述關係,您甚至可以簡化連接:

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

Furthmore你並不需要,如果你通過TeacherRepository對象創建QueryBuilder對象調用from()方法:

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

首先,我們從教師表中選擇所有,然後加入生。假設你在教師模型中的關係名稱是學生。在庫文件:

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

然後在控制器叫它:

$teachersWithStudents = $teacher->getWithStudents();

或在這種情況下,你可以

$teachersWithStudents = $teacher->getStudents();

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

它的工作完美。

0

假設你有兩個tables.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); 
相關問題