Ask your Symfony questions! Pay money and get answers fast! (more info)

Filtering form / data in frontend Symfony

  • SOLVED

Hello,

<strong>I want to propose to my visitors a filtering system, like in the backend where we can filter / sort data.</strong>

In order to place the context, i can link you a page that need the filtering process : [[LINK href="http://www.seek-team.com/en/teams/counter-strike-source/"]]Seek Team[[/LINK]]

I will add a bar to let the user filter on these criteria :
Level / Location / Category / is_premium / Platform


I've found a similar question but the code weren't clean enough (especially, on the pagination). It's my first symfony application, and i'm a student, so i'm willing to learn from you. Don't hesitate to be the more precise as you can.

I can provide you some code (if you need something particular, please ask) :

[[LINK href="http://pastebin.com/q75r8NYe"]]SCHEMA.yml[[/LINK]]
<strong>EDIT : [[LINK href="http://pastebin.com/hzqurRjN"]]stGameTable / stGame / action.class.php[[/LINK]]</strong>
<strong>and i forgot the principal : [[LINK href="http://pastebin.com/uWuUBhzJ"]]addActiveJobsQuery functionJ[[/LINK]]</strong>
<em>Beware, the results must use doctrine pager because there are a lot of results</em>

(just to learn a little more, because i'm not going to implement it, how to filter the results directly on location just by getCulture() - For example, if an american goes on the website, the filter will be active now and not show the french jobs ?)

Thanks (SF 1.4 / Doctrine)

Answers (1)

2010-09-22

Jarret Minkler answers:

Basically what you want to do is setup the query sent to the pager by just changing the where clause.

Are you currently using Doctrine pager? I'll assume yes.

(using the example at the doctrine site)

// Defining initial variables

// An array of your field names
$filters = array(
'Level' => null,
'Location' => null,
'Category' => null,
'is_premium' => null,
'Platform' => null
);

// Build up the where clause ( Level = :Level etc)
$where = array();
foreach ($filters as $filter) {
$where[$filters] = " $filter = :$filter";
$filters[$filter] = $request->getParameter($filter);
}

$where_clause = implode(' AND ', $where); # join each with AND

$currentPage = 1;
$resultsPerPage = 50;

// Creating pager object
$pager = new Doctrine_Pager(
Doctrine_Query::create()
->from( 'User u' )
->leftJoin( 'u.Group g' )
->where($where_clause, $filters) #added this
->orderby( 'u.username ASC'),
$currentPage, // Current page of request
$resultsPerPage // (Optional) Number of results per page. Default is 25
);


Jarret Minkler comments:

http://www.doctrine-project.org/documentation/manual/1_1/hu/utilities

Also has a filtering example, notice the where clause


$pagerLayout = new Doctrine_Pager_Layout(
new Doctrine_Pager(
Doctrine_Query::create()
->from( 'User u' )
->leftJoin( 'u.Group g' )
->where('LOWER(u.username) LIKE LOWER(?)', array( '%'.$_GET['search'].'%' ) )
->orderby( 'u.username ASC' ),
$currentPage,
$resultsPerPage
),
new Doctrine_Pager_Range_Sliding(array(
'chunk' => 5
)),
'http://wwww.domain.com/app/User/list/page,{%page_number}?search={%search}'
);


anxious comments:

<strong>What about the view please ? in order to test your solution</strong>

I've added my action.class.php and some stuff

[[LINK href="http://pastebin.com/hzqurRjN"]]http://pastebin.com/hzqurRjN[[/LINK]]

;)


Jarret Minkler comments:

Think that's beyond the scope of $10. Send me a private message and we can work it out.


anxious comments:

i've sent you via your profile my skype.
Thanks