[question] order of listener method #5391

Closed
opened 2026-01-22 15:06:09 +01:00 by admin · 4 comments
Owner

Originally created by @johndodev on GitHub (Jan 20, 2017).

Originally assigned to: @lcobucci on GitHub.

Hello,

I didn't found the answer on the doc so there is the question.

If I have a listener which use annotation, I can have for example multiple "prepersist method" :

<?php 

class FooListener
{
    /**
    * @ORM\PrePersist()
    */
    public function methodOne($object, $event) {}

    /**
    * @ORM\PrePersist()
    */
    public function methodTwo($object, $event) {}
}

The question is, is the order of calls of theses methods "known" and always the same ? I always assumed reflection is used and the order is the order in the file, but is this true ?

(and subquestion, is it a bad practice to do like this ?)

Originally created by @johndodev on GitHub (Jan 20, 2017). Originally assigned to: @lcobucci on GitHub. Hello, I didn't found the answer on the doc so there is the question. If I have a listener which use annotation, I can have for example multiple "prepersist method" : ``` <?php class FooListener { /** * @ORM\PrePersist() */ public function methodOne($object, $event) {} /** * @ORM\PrePersist() */ public function methodTwo($object, $event) {} } ``` The question is, is the order of calls of theses methods "known" and always the same ? I always assumed reflection is used and the order is the order in the file, but is this true ? (and subquestion, is it a bad practice to do like this ?)
admin added the Question label 2026-01-22 15:06:09 +01:00
admin closed this issue 2026-01-22 15:06:09 +01:00
Author
Owner

@Ocramius commented on GitHub (Jan 20, 2017):

@johndodev the order is not guaranteed, as we have no regression testing nor specification around it.

@Ocramius commented on GitHub (Jan 20, 2017): @johndodev the order is not guaranteed, as we have no regression testing nor specification around it.
Author
Owner

@lcobucci commented on GitHub (Jan 20, 2017):

The question is, is the order of calls of theses methods "known" and always the same ? I always assumed reflection is used and the order is the order in the file, but is this true ?

@johndodev it uses reflection indeed but there's no way to guarantee the order.
If you want to create some sort of dependency I'd suggest you to just have one listener that triggers multiple private methods in the right order.

is it a bad practice to do like this?

What exactly are you doing (or thinking to do)?

@lcobucci commented on GitHub (Jan 20, 2017): > The question is, is the order of calls of theses methods "known" and always the same ? I always assumed reflection is used and the order is the order in the file, but is this true ? @johndodev it uses reflection indeed but there's no way to guarantee the order. If you want to create some sort of dependency I'd suggest you to just have one listener that triggers multiple private methods in the right order. > is it a bad practice to do like this? What exactly are you doing (or thinking to do)?
Author
Owner

@johndodev commented on GitHub (Jan 20, 2017):

Okay, I guess I have the answer.

@lcobucci I just have some listeners which calculate some fields value of business logic entities, for example for a ticket (like a message to a support team), I may automatically assign a team member for this one, but to do that I may need another calculated field like the role of the author, calculated in another method.

I think I will change some habit, thanks.

Maybe a note here http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners-class would'nt be a bad idea.

@johndodev commented on GitHub (Jan 20, 2017): Okay, I guess I have the answer. @lcobucci I just have some listeners which calculate some fields value of business logic entities, for example for a ticket (like a message to a support team), I may automatically assign a team member for this one, but to do that I may need another calculated field like the role of the author, calculated in another method. I think I will change some habit, thanks. Maybe a note here http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners-class would'nt be a bad idea.
Author
Owner

@lcobucci commented on GitHub (Jan 20, 2017):

@johndodev I'd suggest you to keep your business logic very far from the ORM event system and use an explicit event dispatcher to handle things.

Maybe a note here http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners-class would'nt be a bad idea.

Could send us a PR with suggestions?

@lcobucci commented on GitHub (Jan 20, 2017): @johndodev I'd suggest you to keep your business logic very far from the ORM event system and use an explicit event dispatcher to handle things. > Maybe a note here http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners-class would'nt be a bad idea. Could send us a PR with suggestions?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5391