Symfony bundle wich helps to integrate an application with Searcher core library
How to pass my models and imposers to searcher?
parameters:
my_search.context_id: peopleContext
First of all create a parameter, which will allow to group models and imposers into collections
my_search.model_colection:
class: KGzocha\Searcher\Model\FilterModel\Collection\NamedFilterModelCollection
tags:
- { name: searcher.named_filter_model_collection, contextId: %my_search.context_id% }
We are using NamedFilterModelCollection to ease integration with Forms ;)
my_search.imposer_collection:
class: KGzocha\Searcher\FilterImposer\Collection\FilterImposerCollection
tags:
- { name: searcher.filter_imposer_collection, contextId: %my_search.context_id% }
my_search.age_range_model:
class: \AgeRangeModel
tags:
- {
name: searcher.named_model,
contextId: %my_search.context_id%,
modelName: ageRange
}
We will use this name in form
my_search.age_range_imposer:
class: \AgeRangeImposer
tags:
- {
name: searcher.filter_imposer,
contextId: %my_search.context_id%
}
my_search.searching_context:
class: KGzocha\Searcher\Context\QueryBuilderSearchingContext
arguments:
- @my_search.query_builder
my_search.query_builder:
class: Doctrine\ORM\QueryBuilder
factory: [@doctrine.orm.entity_manager, createQueryBuilder]
# or "factory_service" and "factory_method"
calls:
- [select, [p]]
- [from, [\PeopleEntity, p]]
If you don't have already a QueryBuilder:
my_search.searcher:
class: KGzocha\Searcher\Searcher\Searcher
tags:
- {
name: searcher.factory,
contextId: %my_search.context_id%
}
Now we can create form to allow Symfony to populate our models from Request
use KGzocha\Bundle\SearcherBundle\Form\SearchForm;
class MySearchForm extends SearchForm
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('minimalAge', 'integer', [
'property_path' => $this->getPath('ageRange', 'minimalAge'),
])
->add('maximalAge', 'integer', [
'property_path' => $this->getPath('ageRange', 'maximalAge'),
])
/** and any other fields.. **/
->add('<PARAM NAME IN REQUEST>', '<ANY FORM TYPE>', [
'property_path' => $this->getPath(
'<MODEL NAME FROM CONFIG>',
'<MODELS ATTRIBUTE NAME>'
),
]);
}
}
public function searchAction(Request $request)
{
$form = $this->createForm(
new MySearchForm(),
$this->get('my_search.imposer_collection')
);
$form->handleRequest($request);
// Now we can check if form is valid
$searcher = $this->get('my_search.searcher');
$results = $searcher->search(
$form->getData(),
$this->get('my_search.searching_context')
);
}
Everything can be improved with your help!
Any ideas and pull requests are welcomed and appreciated.
See GitHub issues for more.