Filtering with symfony2

Is there any open source code (or example) for Symfony2 that can filter a specific model using several parameters? A good example of what I'm looking for can be seen on this Trulia webpage.

http://www.trulia.com/for_sale/30000-1000000_price/10001_zip/

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/0-500_price/wd,dw_amenities/sm_dogs_pets"

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/400-500_price/wd,dw_amenities

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/wd,dw_amenities"

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/400p_price/dw,cs_amenities

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/1p_beds/1p_baths/400p_price/dw,cs_amenities

Notice how the URLs are generated when clicked on the form, I think it uses one controller for all these routes. How it's done? I don’t think that it redirects all possible routes to a specific controller (see below), maybe some kind of dynamic routing?

/**
 * @Route("/for_rent/{state}/{beds}_beds/{bath}_bath/{mix_price}-{max_price}_price /{amenities_list}
 * @Route("/for_rent/{state}/{mix_price}-{max_price}_price/{amenities_list}
 * @Route("/for_rent/{state}/{bath}_bath/{mix_price}-{max_price}_price/{amenities_list}
 * @Route("/for_rent/{state}/{mix_price}_price/{amenities_list}
 * @Route("/for_rent/{state}/{beds}_beds/{bath}_bath/{amenities_list}    
 * ........
 */

public function filterAction($state, $beds, $bath, $min_price, $max_price ....)
{
    ....
}

Thank.

+5
source share
4 answers

(.. , min-max), . , Acme\FooBundle\Entity\Bar:

$em = $this->getDoctrine()->getEntityManager();
$repo = $em->getRepository('AcmeFooBundle:Bar');

$criteria = array(
    'state' => $state,
    'beds' => $beds,
    // and so on...
);
$data = $repo->findBy($criteria);

$criteria , , , , , . $data , .

DQL (, , ) , .

+1

, , Routing , , ? , .

, , DQL , SQL Doctrine . . , DQL.

+1

csg, ( @Route ( "/search/{q}), " ". , URL: http://www.trulia.com/for_sale/30000-1000000_price/10001_zip/

@Route("/search/{q} URL- .

0

, LexikFormFilterBundle "lexik/form-filter-bundle": "~2.0", DQL , .

Bundle, , FormType (, , SencioGeneratorBundle). , FilterForm, DQL ( Lexik).

Composer, README.md

All he does is override the Doctrine Type Guesser, which offers the required FormType for each Entity field and replaces the type with the corresponding LexikFormFilterType. For example, instead of a simple one it is NumberTypeused filter_number, which displays in the form of two numbers, intervals of the interval Max and Min.

private function createFilterForm($formType)
{
    $adapter = $this->get('dd_form.form_adapter');
    $form = $adapter->adaptForm(
        $formType,
        $this->generateUrl('document_search'),
        array('fieldToRemove1', 'fieldToRemove2')
    );
    return $form;
}

In the Submit form, you simply pass it to Lexik and run the generated request, as shown in my example.

public function searchAction(Request $request)
{
    // $docType = new FormType/FQCN() could do too.
    $docType = 'FormType/FQCN';
    $filterForm = $this->createFilterForm($docType);
    $filterForm->handleRequest($request);

    $filterBuilder = $this->getDocRepo($docType)
        ->createQueryBuilder('e');
    $this->get('lexik_form_filter.query_builder_updater')
        ->addFilterConditions($filterForm, $filterBuilder);

    $entities = $filterBuilder->getQuery()->execute();

    return array(
        'entities'   => $entities,
        'filterForm' => $filterForm->createView(),
    );
}
0
source

All Articles