Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9301371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:00:00+00:00 2026-06-18T23:00:00+00:00

I’m creating a form that modifies the query of KnpPaginatorBundle in order to show

  • 0

I’m creating a form that modifies the query of KnpPaginatorBundle in order to show filtered paginated results.

To do that. When the form is valid I build the querystring (concatenating) with the required fields to filter. I set $filteredDql variable with my custom query. The problem is that it’s value only remains for the first page. When I change the page it turns to NULL. And pagination resets…

I think, the problem could be that I’m setting the $filteredDql variable in a block context (when the form is valid only).

How I can set $filteredDql variable for the whole operation or application wide ? maybe using parameters ? I tried using the container from the controller without success using:

$this->container->setParameter('key', value);
$this->container->getParameter('key');
$this->container->HasParameter('key');

But this way I’m getting 500 Internal Server Error

Here’s the code:

public function indexAction(Request $request, $page)
{
    $filters = new Filters();

    $form = $this->createForm(new FiltersType(), $filters);

    $dql = "SELECT a FROM ViciousAmateurBundle:Post a WHERE a.is_active = true";

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            $country = $filters->getCountry();
            $city = $filters->getCity();
            $gender = $filters->getGender();
            $sexualOrientation = $filters->getSexualOrientation();

            if (isset($country)) {
                $dql .= " AND a.country = '" . $filters->getCountry() . "'";
            }
            if (isset($city)) {
                $dql .= " AND a.city = '" . $filters->getCity() . "'";
            }
            if (isset($gender)) {
                $dql .= " AND a.gender = '" . $filters->getGender() . "'";
            }
            if (isset($sexualOrientation)) {
                $dql .= " AND a.sexual_orientation = '" . $filters->getSexualOrientation() . "'";
            }
            $filteredDql = $dql;
        }
    }

    $em = $this->get('doctrine.orm.entity_manager');

    if (isset($filteredDql)) {
        $query = $em->createQuery($filteredDql);
    } else {
        $query = $em->createQuery($dql);
    }

    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query,
        $this->get('request')->query->get('page', $page),
        5
    );

    return $this->render('ViciousAmateurBundle:Default:index.html.twig', array(
        'form' => $form->createView(),
        'pagination' => $pagination
        )
    );
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:00:01+00:00Added an answer on June 18, 2026 at 11:00 pm

    Finally solved it using a session variable to store the current filters. Sessions are application wide or stateless. I’m quite happy with this approach 😀

    Although, It’s a shame that KnpPaginatorBundle can’t remember custom query “filters” (WHERE, GROUP BY, …) through pagination by default when data comes from form validation :S And just support it by using the sortable functions to make links to sort asc or desc its a shame…

    Here’s the code:

    /**
     * @Route("/{page}", defaults={"page" = 1}, name="homepage")
     * @Route("/")
     * @Template()
     */
    public function indexAction(Request $request, $page)
    {
        $filters = new Filters();
    
        $form = $this->createForm(new FiltersType(), $filters);
    
        $session = $this->getRequest()->getSession();
    
        if ($session->get('dql') == null) {
            $session->set('dql', "SELECT a FROM ViciousAmateurBundle:Post a WHERE a.is_active = true");
        }
    
        if ($request->isMethod('POST')) {
            $form->bind($request);
    
            if ($form->isValid()) {
                $dql = "SELECT a FROM ViciousAmateurBundle:Post a WHERE a.is_active = true";
                $country = $filters->getCountry();
                $city = $filters->getCity();
                $gender = $filters->getGender();
                $sexualOrientation = $filters->getSexualOrientation();
    
                if (isset($country)) {
                    $dql .= " AND a.country = '" . $filters->getCountry() . "'";
                }
                if (isset($city)) {
                    $dql .= " AND a.city = '" . $filters->getCity() . "'";
                }
                if (isset($gender)) {
                    $dql .= " AND a.gender = '" . $filters->getGender() . "'";
                }
                if (isset($sexualOrientation)) {
                    $dql .= " AND a.sexual_orientation = '" . $filters->getSexualOrientation() . "'";
                }
    
                $session->set('dql', $dql);
            }
        }
    
        $em = $this->get('doctrine.orm.entity_manager');
    
        $query = $em->createQuery($session->get('dql'));
    
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $query,
            $this->get('request')->query->get('page', $page),
            5
        );
    
        return $this->render('ViciousAmateurBundle:Default:index.html.twig', array(
            'form' => $form->createView(),
            'pagination' => $pagination
            )
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I want to show the soap response to UIWebview.. my soap response is, <p><img
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.