Zeljko
Zeljko

Reputation: 5158

Symfony2: How to create event listener thru entity annotations?

I would like to have custom annotations on my entity that will do something on persist event, but defined thru annotations. An example:

/**
* @ORM\Entity
* @Searchable("column"="title")
*/
class Product
{

}

That @Searchable annotation should somehow trigger my code that will read column title, separate words and save into its own table (much simplified example of what I really need).

I am aware I can use doctrine listeners and register them in config.yml but I really need this to be done thru entity annotations so that searchable bundle could be easily reused thru different projects. I read tons of docs, even tried to understand Gedmo behaviours bundle code but I failed. Can somehow show me how to do this, in an idiot-proof way?

I even read http://php-and-symfony.matthiasnoback.nl/2011/12/symfony2-doctrine-common-creating-powerful-annotations/ but that was kinda too complicated.

Upvotes: 0

Views: 3048

Answers (1)

Florian Klein
Florian Klein

Reputation: 8915

The link you gave is a perfect example of how you should do it. How much more complicated do you think it would be with a custom hand crafted solution?

Still, you could try to:

Upvotes: 1

Related Questions