macnibblet / MCNElasticSearch

Provides a simple way to connect doctrine ORM to elastic search
BSD 3-Clause "New" or "Revised" License
19 stars 7 forks source link

Update the readme with instructions for use with the new 1.0.0 branch #9

Open macnibblet opened 9 years ago

rasarmento commented 9 years ago

If i am right, the Elastica was removed, and the Synchroniser from the step two of the Readme no longer exists. Now i am lost, do you have some working code i can use as example?

macnibblet commented 9 years ago

Since docrine added entity listeners the synchronizer is no longer required. So what you have to do is register a entity listener, inject the document service and then just listen to update/delete/persist events

use Company\Entity\CompanyEntity;
use Doctrine\ORM\Event\LifecycleEventArgs;
use MCNElasticSearch\Service\DocumentService;

/**
 * Class CompanyEntityListener
 *
 * Listens for changes to the entity and updates elastic search with them
 */
class CompanyEntityListener
{
    /**
     * @var \MCNElasticSearch\Service\DocumentService
     */
    private $documentService;

    /**
     * @param DocumentService $documentService
     */
    public function __construct(DocumentService $documentService)
    {
        $this->documentService = $documentService;
    }

    public function remove(CompanyEntity $company, LifecycleEventArgs $event)
    {
        $this->documentService->delete($company);
    }

    public function update(CompanyEntity $company, LifecycleEventArgs $event)
    {
        $this->documentService->update($company);
    }

    public function persist(CompanyEntity $company, LifecycleEventArgs $event)
    {
        $this->documentService->add($company);
    }
}
rasarmento commented 9 years ago

Ok, i've created the EntityListener, and used it through the annotation @ORM\EntityListeners({"CompanyEntityListener"}), but i don't know how to instantiate the $documentService. :(