0

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.

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

5 Comments

That example doesn't explain how to trigger it on postPersist event. Gedmo/Timestampable behaviour does that but the code is kinda confusing for me.
it uses doctrine subscribers/listeners (see my link #1)
this listener could be a service that have access to annotation reader (injected in ctor for example).
Ok, the idea is: I create a bundle with postPersist listener (I already know them). In that method, I should put the code that will check annotations of the entity that is persisted and do something with that? The question is: that would be triggered for every entity that is persisted. Isn't it kinda slow? I only want that to be triggered on entities that have my annotation. Is it even possible?
In theory, the annotations are cached if you use the correct preconfigured annotation reader. And no, it's not possible to listen only on some entities. You have to do that check at runtime. What can be cached thus, is that an entity should be processed or not by caching the annotation somewhere. But as I said, it should already be done by the annotation reader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.