FOSElasticaBundle/Resources/doc/cookbook/custom-repositories.md

77 lines
1.8 KiB
Markdown
Raw Normal View History

2013-12-17 11:09:58 +01:00
##### Custom Repositories
As well as the default repository you can create a custom repository for an entity and add
methods for particular searches. These need to extend `FOS\ElasticaBundle\Repository` to have
access to the finder:
```php
2013-12-17 11:09:58 +01:00
<?php
namespace Acme\ElasticaBundle\SearchRepository;
use FOS\ElasticaBundle\Repository;
class UserRepository extends Repository
{
public function findWithCustomQuery($searchText)
{
// build $query with Elastica objects
$this->find($query);
}
}
```
To use the custom repository specify it in the mapping for the entity:
```yaml
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
website:
client: default
types:
user:
mappings:
# your mappings
persistence:
driver: orm
model: Application\UserBundle\Entity\User
provider: ~
finder: ~
repository: Acme\ElasticaBundle\SearchRepository\UserRepository
```
2013-12-17 11:09:58 +01:00
Then the custom queries will be available when using the repository returned from the manager:
```php
/** var FOS\ElasticaBundle\Manager\RepositoryManager */
$repositoryManager = $container->get('fos_elastica.manager');
2013-12-17 11:09:58 +01:00
/** var FOS\ElasticaBundle\Repository */
$repository = $repositoryManager->getRepository('UserBundle:User');
2013-12-17 11:09:58 +01:00
/** var array of Acme\UserBundle\Entity\User */
$users = $repository->findWithCustomQuery('bob');
```
2013-12-17 11:09:58 +01:00
Alternatively you can specify the custom repository using an annotation in the entity:
```php
2013-12-17 11:09:58 +01:00
<?php
namespace Application\UserBundle\Entity;
use FOS\ElasticaBundle\Annotation\Search;
2013-12-17 11:09:58 +01:00
/**
* @Search(repositoryClass="Acme\ElasticaBundle\SearchRepository\UserRepository")
*/
class User
{
//---
}
```