Reputation: 1082
I am struggling with something that may sound stupid but :
I created a listener class that add content to Lucene Search Index after an entity is persisted.
My config.yml :
services:
searchindexer.listener:
class: ripr\WfBundle\Listener\SearchIndexer
tags:
- { name: doctrine.event_listener, event: postPersist }
My file is in src/ripr/WfBundle/Listener
Filename is SearchIndexer.php
Class name is SearchIndexer
Namespace is ripr\WfBundle\Listener
I get an error on this line
$index = $this->get('ivory_lucene_search')->getIndex('identifier1');
error :
Fatal error: Call to a member function get() on a non-object
This sounds to me that class is not loaded. I can't get where I am wrong.
I flush dev cache multiples times.
This precise line works perfectly when I call it inside a classic Controller.
--------Edited--------
My class code to help
<?php
namespace ripr\WfBundle\Listener;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\Event\LifecycleEventArgs;
use ripr\WfBundle\Entity\Item;
use Ivory\LuceneSearchBundle\Model\Document;
use Ivory\LuceneSearchBundle\Model\Field;
/**
* Search indexer.
*
*
*/
class SearchIndexer extends Controller
{
/**
* Index content
*
*/
public function postPersist (LifeCycleEventArgs $args) {
// $luceneSearch = $this->get('ivory_lucene_search');
// $luceneSearch->eraseIndex('identifier1');
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();
// perhaps you only want to act on some "Product" entity
$index = $this->get('ivory_lucene_search')->getIndex('identifier1');
$document = new Document();
$document->addField(Field::text('titre', 'bille'));
$document->addField(Field::text('texte', 'billou'));
// Add your document to the index
$index->addDocument($document);
$index->commit();
}
}
Upvotes: 0
Views: 820
Reputation: 17678
This is happening because you don't have access to the container inside your listener class by default. However, don't just extend your class from Controller
(which does have access to the container) or inject the entire container. Instead, inject only the services you need into it:
config.yml:
services:
searchindexer.listener:
class: ripr\WfBundle\Listener\SearchIndexer
arguments:
luceneSearch: "@ivory_lucene_search"
tags:
- { name: doctrine.event_listener, event: postPersist }
SearchIndexer.php:
class SearchIndexer
{
protected $luceneSearch;
public function __constructor($luceneSearch)
{
$this->luceneSearch = $luceneSearch;
}
// ...
Then, anywhere you need to use the lucene search inside the listener, you can simply call $this->luceneSearch
.
Upvotes: 3