Symfony 3.4 - 我有使用knp-paginator-bundle的问题

时间:2018-05-15 14:55:09

标签: symfony-3.4

我有以下控制器

/**
*
* @Route("/searchname" , name="search_student_name")
*
*/
public function SearchStudentName(Request $request, SessionInterface $sessionName)
{
    $params = $request->request->all();
    $SName = $params['txtSearchName'];

    if (isset($SName)){
        $sessionName->set('SName', $SName);
    }

    $em = $this->getDoctrine()->getManager();
    $query = $em->createQuery("SELECT s FROM AppBundle:Student s WHERE s.name LIKE :Name ORDER BY s.name");
    $query->setParameter('Name', '%'.$sessionName->get('SName').'%');

    $students = $query->getArrayResult();

    $paginator = $this->get('knp_paginator');
    $result = $paginator->paginate(
      $students,
      $request->query->get('page', 1)/*page number*/,
      50/*limit per page*/
    );

    return $this->render('student/index.html.twig', array(
        'students' => $result,
    ));
}

第一页显示正确。 但是当我试图去其他页面时

app_dev.php/student/searchname?page=2

我有以下错误:

  

NotFoundHttpException

     

找不到HTTP 404

     

@ParamConverter注释找不到AppBundle \ Entity \ Student对象。

我有indexAction,适用于任何页面:

  

唯一的区别是$ students = $ em-> getRepository('AppBundle:Student') - > findAll();

     

并使用session来保存名称

/**
 * Lists all student entities.
 *
 * @Route("/", name="student_index")
 * @Method("GET")
 */
public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $students = $em->getRepository('AppBundle:Student')->findAll();

    /**
    * @var $paginator \Knp\Component\Pager\Paginator
    */
    $paginator = $this->get('knp_paginator');
    $result = $paginator->paginate(
      $students,
      $request->query->get('page', 1)/*page number*/,
      50/*limit per page*/
    );

    return $this->render('student/index.html.twig', array(
        'students' => $result,
    ));
}

有人可以帮我解决这个问题吗?

Session value

1 个答案:

答案 0 :(得分:0)

也许$ sessionName-> get('SName')为空或为空?你可以尝试为会话设置一个默认值:

$sessionName->get('SName', 'something default');