Skip to content

Latest commit

 

History

History
56 lines (45 loc) · 1.55 KB

custom-repositories.md

File metadata and controls

56 lines (45 loc) · 1.55 KB
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

namespace Acme\ElasticaBundle\SearchRepository;

use FOS\ElasticaBundle\Repository;

class UserRepository extends Repository
{
    public function findWithCustomQuery($searchText)
    {
        // build $query with Elastica objects
        return $this->find($query);
    }
}

To use the custom repository specify it in the mapping for the entity:

fos_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        app:
            client: default
            types:
                user:
                    properties:
                        # your properties
                    persistence:
                        driver: orm
                        model: Application\UserBundle\Entity\User
                        provider: ~
                        finder: ~
                        repository: Acme\ElasticaBundle\SearchRepository\UserRepository

Then the custom queries will be available when using the repository returned from the manager:

/** var FOS\ElasticaBundle\Manager\RepositoryManager */
$repositoryManager = $container->get('fos_elastica.manager');

/** var FOS\ElasticaBundle\Repository */
$repository = $repositoryManager->getRepository('UserBundle:User');

/** var array of Acme\UserBundle\Entity\User */
$users = $repository->findWithCustomQuery('bob');