2014-05-19 82 views
0

我正在尋找顯示與當前用戶(導師)具有相同課程ID的學生列表。自定義查詢symfony2

http://snag.gy/VOHJ3.jpg這是我的數據庫設計。

<?php 

namespace Simple\ProfileBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\HttpFoundation\Request; 

class SecurityController extends Controller 
{ 
public function loginAction(Request $request) 
{ 

    $session = $request->getSession(); 

    // get the login error if there is one 
    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { 
     $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); 
    } else { 
     $error = $session->get(SecurityContext::AUTHENTICATION_ERROR); 
     $session->remove(SecurityContext::AUTHENTICATION_ERROR); 
    } 

    return $this->render('SimpleProfileBundle:Security:login.html.twig', array(
     // last username entered by the user 
     'last_username' => $session->get(SecurityContext::LAST_USERNAME), 
     'error'   => $error, 
       )); 
} 

public function dumpStringAction() 
{ 

$findStudents = $this->getUser()->getCourses(); 




$results = $this->_em 
->createQuery("SELECT * FROM user where") 
->getResult(); 

return $results; 
} 

return $this->render('SimpleProfileBundle:Security:dumpString.html.twig', array(  
'findstudents'=> $findStudents)); 



} 

} 

任何人都有任何想法我可以做到這一點?我正在考慮自定義查詢,但我不確定如何執行此操作?

乾杯

回答

0

首先,如果你想使用自定義查詢,你應該做的是通過創建實體的存儲庫。

例子:

實體:

<?php 

namespace YourName\YourBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* YourClass 
* 
* @ORM\Entity(repositoryClass="YourName\YourBundle\Entity\Repository\YourClassRepository") 
* @ORM\Table(name="your_class") 
*/ 
class YourClass 
{ 
    // your entity definition 
} 

然後,你必須創建實體倉儲類:

<?php 

namespace YourName\YourBundle\Entity\Repository; 

use Doctrine\ORM\EntityRepository; 

/** 
* YourClassRepository 
*/ 
class YourClassRepository extends EntityRepository 
{ 
    public function getStudentsByCourseID($courseId) 
    { 
     $qb = $this->_em->createQueryBuilder(); 
     $qb 
      ->select('student') 
      ->from('YourNameYourBundle:YourClass', 'student') 
      ->leftJoin('YourNameYourBundle:Course', 'course') 
      ->where('course.id == :courseId'); 

     $qb->setParameter('courseId', $courseId); 

     return $qb->getQuery()->getArrayResult(); 

} 

然後就可以調用您的自定義查詢在你的控制器:

<?php 

namespace Simple\ProfileBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\HttpFoundation\Request; 

class SecurityController extends Controller 
{ 
    public function loginAction(Request $request) 
    { 
     // your code here... 
    } 


    public function yourAction($courseID) 
    { 
     $repo = $this->getDoctrine()->getRepository('YourNameYourBundle:YourClass'); 
     $students = $repo->getStudentsByCourseID($courseID); 

     return [ 
      'students' => $students 
     ]; 
    } 
} 

我認爲這就是你需要的。

+0

Aah運行SQL查詢的日子是4行代碼。不再! – Rudie

+0

您也可以僅從控制器執行SQL,但這不是一個好習慣。 – bartek

+0

感謝您的回覆,這非常有幫助,當您根據數據庫信息找到用戶類型時,我將需要在查詢中顯示多個「where」。例如,它會是course.id = x和role.id = x,可以這樣做嗎? – user2171245