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:
|
|
|
|
|
2014-11-21 14:21:26 +01:00
|
|
|
```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:
|
|
|
|
|
2014-11-21 14:21:26 +01:00
|
|
|
```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:
|
|
|
|
|
2014-11-21 14:21:26 +01:00
|
|
|
```php
|
|
|
|
/** var FOS\ElasticaBundle\Manager\RepositoryManager */
|
|
|
|
$repositoryManager = $container->get('fos_elastica.manager');
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2014-11-21 14:21:26 +01:00
|
|
|
/** var FOS\ElasticaBundle\Repository */
|
|
|
|
$repository = $repositoryManager->getRepository('UserBundle:User');
|
2013-12-17 11:09:58 +01:00
|
|
|
|
2014-11-21 14:21:26 +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:
|
|
|
|
|
2014-11-21 14:21:26 +01:00
|
|
|
```php
|
2013-12-17 11:09:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Application\UserBundle\Entity;
|
|
|
|
|
2014-08-18 13:30:45 +02:00
|
|
|
use FOS\ElasticaBundle\Annotation\Search;
|
2013-12-17 11:09:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Search(repositoryClass="Acme\ElasticaBundle\SearchRepository\UserRepository")
|
|
|
|
*/
|
|
|
|
class User
|
|
|
|
{
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
|
|
|
}
|
2014-11-21 14:21:26 +01:00
|
|
|
```
|