简体   繁体   中英

Custom query symfony2

I am looking to display a list of students that have the same course ID as the current user (tutor).

http://snag.gy/VOHJ3.jpg Here is my database design.

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



}

}

Anyone have any idea how i can do this ? I was thinking of a custom query however i am unsure how to do so?

Cheers

First of all if you want to use custom queries, you should do that by creating entities' repository.

Example:

Entity:

<?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
}

Then you have to create entity repository class:

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

}

Then you can call your custom query in your controller:

<?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
        ];
    }
}

I think that's what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM