会话的SYMFONY Kpn分页器

时间:2018-06-20 07:34:11

标签: knpmenubundle

所有人

我已经安装了KNP捆绑式分页器,效果很好,除了当我使用可以过滤结果的表格时,还可以,但是当我想转到下一页时,可以简化我的过滤...

我的控制器的代码

<?php

namespace AdminBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;


use Symfony\Bridge\Doctrine\Form\Type\EntityType;

use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints as Assert;

use AdminBundle\Entity\Products;
use AdminBundle\Form\ProductsType;

class ProductsController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction($page, Request $request, $data)
    {



        $form = $this->createFormBuilder()


                    ->add('category', 'entity', array('class' => 'AdminBundle:Category',  'label' => false, 'choice_label' => 'name', 'required' => false,'expanded' => true,  'placeholder' => 'Tous', 'multiple' => false))
                    ->add('preservation', 'entity', array('class' => 'AdminBundle:Preservation', 'choice_label' => 'name', 'label' => false, 'required' => false, 'expanded' => true,  'placeholder' => 'Tous'))
                    ->add('population', 'entity', array('class' => 'AdminBundle:Population',  'label' => false, 'choice_label' => 'name', 'required' => false,'expanded' => true,  'placeholder' => 'Tous'))
                    ->add('innovation', 'entity', array('class' => 'AdminBundle:Innovation', 'choice_label' => 'name', 'label' => false, 'required' => false, 'expanded' => true,  'placeholder' => 'Tous'))
                    ->add('timeframe', 'entity', array('class' => 'AdminBundle:Timeframe', 'choice_label' => 'name', 'label' => false, 'required' => false, 'expanded' => true,  'placeholder' => 'Tous'))
                    ->add('component', 'entity', array('class' => 'AdminBundle:Component', 'choice_label' => 'name', 'label' => false, 'required' => false, 'expanded' => true,  'placeholder' => 'Tous'))
                    ->add('country', 'entity', array('class' => 'AdminBundle:Country', 'choice_label' => 'name', 'label' => 'COUNTRY', 'required' => false, 'multiple' =>false, 'placeholder' =>'TOUS LES PAYS'))
                    ->add('search', TextType::class, array('required' => false,  'label'       => "MOT CLEF "))

                     ->add('submit', SubmitType::class, [
                         'label' => "VALIDER VOTRE RECHERCHE",
                     ])
                     ->getForm();

        $form->handleRequest($request);

        if($form->isSubmitted() &&  $form->isValid()){
            $data = $form->getData(); 
        }
        $repository=$this
            ->getDoctrine()
            ->getManager()
            ->getRepository('AdminBundle:Products');

            $listeproduits=$repository->ListAll($data,$page);

            $paginator  = $this->get('knp_paginator');

            $pagination = $paginator->paginate(
            $listeproduits, /* query NOT result */
            $request->query->getInt('page', 1)/*page number*/,
            10/*limit per page*/
            );

            return $this->render('AdminBundle:Advert:produits-drupal.html.twig', array('page'=>$page, 'pagination' => $pagination, 'form'=>$form->createView()));


    }
}

和存储库

<?php

namespace AdminBundle\Repository;


/**
 * ProductsRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ProductsRepository extends \Doctrine\ORM\EntityRepository
{

    public function ListAll($data, $page)
    {


                    $query = $this->createQueryBuilder('a')->orderBy('a.updated','DESC');

                    // CATEGORY
if($data['category'] != '')
{
$query->innerJoin('a.category', 'cat')->andWhere('cat.id IN (:catids)')->setParameter('catids', $data['category']);

//$query->leftJoin('a.category', 'category')->setParameter('category', $data['category']);

}

// PRESERVATION

if($data['preservation'] != '')
{
    $query->innerJoin('a.preservation', 'pre')->andWhere('pre.id IN (:preids)')->setParameter('preids', $data['preservation']);

//$query->andWhere('a.preservation = :preservation')->setParameter('preservation', $data['preservation']);
}

// POPULATION

if($data['population'] != '')
{
    $query->innerJoin('a.population', 'pop')->andWhere('pop.id IN (:popids)')->setParameter('popids', $data['population']);
//$query->andWhere('a.population = :population')->setParameter('population', $data['population']);
}

// TIMEFRAME

if($data['timeframe'] != '')
{
    $query->innerJoin('a.timeframe', 'tim')->andWhere('tim.id IN (:timids)')->setParameter('timids', $data['timeframe']);
//$query->andWhere('a.timeframe = :timeframe')->setParameter('timeframe', $data['timeframe']);
}

// INNOVATION

if($data['innovation'] != '')
{
    $query->innerJoin('a.innovation', 'ino')->andWhere('ino.id IN (:inoids)')->setParameter('inoids', $data['innovation']);
//$query->andWhere('a.innovation = :innovation')->setParameter('innovation', $data['innovation']);
}

// COMPONENT

if($data['component'] != '')
{
    $query->innerJoin('a.component', 'com')->andWhere('com.id IN (:comids)')->setParameter('comids', $data['component']);
//$query->andWhere('a.component = :component')->setParameter('component', $data['component']);
}

// KEYWORDS

//if($data['reason_text'] != '')
//{
//$query->andWhere('a.reason_text LIKE :descriptionlongnat')
//->setParameter('descriptionlongnat', "%$reason_text%");
//}

// COUNTRY

//if($data['search'] !== null)
//{
//  $query->andWhere('a.descriptionshortnat LIKE :descriptionshortnat')->setParameter('descriptionshortnat', '%'.$data['search'].'%')->orWhere('a.descriptionlongnat LIKE :descriptionlongnat')->setParameter('descriptionlongnat', '%'.$data['search'].'%')
//  ->orWhere('a.descriptionlongen LIKE :descriptionlongen')->setParameter('descriptionlongen', '%'.$data['search'].'%')->orWhere('a.descriptionshorten LIKE :descriptionshorten')->setParameter('descriptionshorten', '%'.$data['search'].'%')
//  ->orWhere('a.name LIKE :name')->setParameter('name', '%'.$data['search'].'%');
//}

    if($data['search'] != '')
{
    $query->andWhere('a.descriptionshortnat LIKE :searchTerm OR a.descriptionlongnat LIKE :searchTerm OR a.descriptionlongen LIKE :searchTerm OR a.descriptionshorten LIKE :searchTerm OR a.name LIKE :searchTerm')->setParameter('searchTerm', '%'.$data['search'].'%');

}       

if($data['country'] != '')
{
$query->andWhere('a.country = :country')
->setParameter('country', $data['country']);
}



                    return $query->getQuery()->getResult();



    }

}

在树枝中使用$ pagination后。 我需要保留过滤值以随导航页面一起移动。 谢谢

0 个答案:

没有答案