Skip to content

Commit

Permalink
- add filters in FilterExtension
Browse files Browse the repository at this point in the history
- update doc
  • Loading branch information
Cédric G committed Mar 9, 2012
1 parent 68467d0 commit 9430461
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 141 deletions.
8 changes: 6 additions & 2 deletions Filter/Extension/FilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ protected function loadTypes()
return array(
new Type\FieldFilterType(),
new Type\FilterType(),
new Type\TextFilterType(),
new Type\NumberFilterType(),
new Type\BooleanFilterType(),
new Type\CheckboxFilterType(),
new Type\ChoiceFilterType(),
new Type\DateFilterType(),
new Type\NumberFilterType(),
new Type\NumberRangeFilterType(),
new Type\TextFilterType(),
);
}
}
2 changes: 1 addition & 1 deletion Filter/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function addFilerCondition(\Doctrine\ORM\QueryBuilder $queryBuilder, F
call_user_func($callable, $queryBuilder, $form->getName(), $values);
}
} else {
// if no closure we use the applyFilter() method from an FilterTypeInterface
// if no closure we use the applyFilter() method from a FilterTypeInterface
$types = array_reverse($form->getTypes());
$filterApplied = false;
$i = 0;
Expand Down
145 changes: 7 additions & 138 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,146 +2,15 @@ Overview
========

This Symfony2 bundle aims to provide classes to build a form filter and then build a doctrine query from this form filter.
This bundle is not finished yet, we just created a base set of classes.

The idea is:

Installation
============
1. Create a form type extending from `Symfony\Component\Form\AbstractType` as usual
2. Add form fields by using filter types instead of form types (e.g. use `filter_text` instead of `text` type)
3. Then call a service to build the query from the form instance.

Update your `deps` and `deps.lock` files:

// deps
...
[LexikFormFilterBundle]
git=https://github.com/lexik/LexikFormFilterBundle.git
target=/bundles/Lexik/Bundle/FormFilterBundle
Documentation
=============

// deps.lock
...
LexikFormFilterBundle <commit>

Register the namespaces with the autoloader:

// app/autoload.php
$loader->registerNamespaces(array(
// ...
'Lexik' => __DIR__.'/../vendor/bundles',
// ...
));

Register the bundle with your kernel:

// in AppKernel::registerBundles()
$bundles = array(
// ...
new Lexik\Bundle\FormFilterBundle\LexikFormFilterBundle(),
// ...
);


Usage
=====

Here an example of how to use the bundle.
Once the bundle is loaded in your app, add this in your `app/config.yml`

twig:
form:
resources:
- LexikFormFilterBundle:Form:form_div_layout.html.twig


Let's use the following entity:

// MyEntity.php
namespace Project\Bundle\SuperBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class MyEntity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(type="string")
*/
protected $name;

/**
* @ORM\Column(type="integer")
*/
protected $rank;
}

Create a type extended from AbstractType, add `name` and `rank` and use the filter_xxxx types.

// MySuperFilterType.php
namespace Project\Bundle\SuperBundle\Filter;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class MySuperFilterType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'filter_text');
$builder->add('rank', 'filter_number');
}

public function getName()
{
return 'my_super_filter';
}
}

Then in an action, create a form object from the MySuperFilterType. Let's say we filter when the form is submitted with a post method.

// DefaultController.php
namespace Project\Bundle\SuperBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Project\Bundle\SuperBundle\Filter\MySuperFilterType;

class DefaultController extends Controller
{
public function testFilterAction()
{
$form = $this->get('form.factory')->create(new MySuperFilterType());

if ($this->get('request')->getMethod() == 'POST') {
// bind values from the request
$form->bindRequest($this->get('request'));

// initliaze a query builder
$queryBuilder = $this->get('doctrine.orm.entity_manager')
->getRepository('ProjectSuperBundle:MyEntity')
->createQueryBuilder('e');

// build the query from the given form object
$this->get('lexik_form_filter.query_builder')->buildQuery($form, $queryBuilder);

var_dump($queryBuilder->getDql());
}

return $this->render('ProjectSuperBundle:Default:testFilter.html.twig', array(
'form' => $form->createView(),
));
}
}

Display the form

// testFilter.html.twig
<form method="post">
{{ form_rest(form) }}
<input type="submit" name="submit-filter" value="filter" />
</form>
For installation and how to use the bundle refer to [Resources/doc/index.md](https://github.com/lexik/LexikFormFilterBundle/blob/master/Resources/doc/index.md)
141 changes: 141 additions & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
Installation
============

Update your `deps` and `deps.lock` files:

// deps
...
[LexikFormFilterBundle]
git=https://github.com/lexik/LexikFormFilterBundle.git
target=/bundles/Lexik/Bundle/FormFilterBundle

// deps.lock
...
LexikFormFilterBundle <commit>

Register the namespaces with the autoloader:

// app/autoload.php
$loader->registerNamespaces(array(
// ...
'Lexik' => __DIR__.'/../vendor/bundles',
// ...
));

Register the bundle with your kernel:

// in AppKernel::registerBundles()
$bundles = array(
// ...
new Lexik\Bundle\FormFilterBundle\LexikFormFilterBundle(),
// ...
);


Usage
=====

Here an example of how to use the bundle.
Once the bundle is loaded in your app, add this in your `app/config.yml`

twig:
form:
resources:
- LexikFormFilterBundle:Form:form_div_layout.html.twig


Let's use the following entity:

// MyEntity.php
namespace Project\Bundle\SuperBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class MyEntity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(type="string")
*/
protected $name;

/**
* @ORM\Column(type="integer")
*/
protected $rank;
}

Create a type extended from AbstractType, add `name` and `rank` and use the filter_xxxx types.

// MySuperFilterType.php
namespace Project\Bundle\SuperBundle\Filter;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class MySuperFilterType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'filter_text');
$builder->add('rank', 'filter_number');
}

public function getName()
{
return 'my_super_filter';
}
}

Then in an action, create a form object from the MySuperFilterType. Let's say we filter when the form is submitted with a post method.

// DefaultController.php
namespace Project\Bundle\SuperBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Project\Bundle\SuperBundle\Filter\MySuperFilterType;

class DefaultController extends Controller
{
public function testFilterAction()
{
$form = $this->get('form.factory')->create(new MySuperFilterType());

if ($this->get('request')->getMethod() == 'POST') {
// bind values from the request
$form->bindRequest($this->get('request'));

// initliaze a query builder
$queryBuilder = $this->get('doctrine.orm.entity_manager')
->getRepository('ProjectSuperBundle:MyEntity')
->createQueryBuilder('e');

// build the query from the given form object
$this->get('lexik_form_filter.query_builder')->buildQuery($form, $queryBuilder);

// now look at the DQL =)
var_dump($queryBuilder->getDql());
}

return $this->render('ProjectSuperBundle:Default:testFilter.html.twig', array(
'form' => $form->createView(),
));
}
}

Basic template

// testFilter.html.twig
<form method="post">
{{ form_rest(form) }}
<input type="submit" name="submit-filter" value="filter" />
</form>

0 comments on commit 9430461

Please sign in to comment.