自定义查询symfony2

时间:2014-05-19 15:59:16

标签: php symfony

我希望显示与当前用户(导师)具有相同课程ID的学生列表。

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



}

}

任何人都知道我该怎么做?我在想一个自定义查询,但我不确定该怎么做?

干杯

1 个答案:

答案 0 :(得分: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
        ];
    }
}

我认为这就是你所需要的。