Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Doctrine Bridge] fix priority for doctrine event listeners #22001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2017

Conversation

dmaicher
Copy link
Contributor

@dmaicher dmaicher commented Mar 14, 2017

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #21977
License MIT
Doc PR -

This fixes handling the priorities for doctrine event listeners. As found out by @chapterjason in #21977 the priority was incorrectly handled as soon as a listener had more than one tag (so listening to multiple events).

With this changes all tagged listeners are globally sorted by priority (using the same stable sort approach as in the later available PriorityTaggedServiceTrait) and then added one by one to the event manager.

I also updated the tests a bit as it was not covering all cases.

We also have to extend the docs for it I think as it does not mention the priority and lazy option at all? http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html

@dmaicher dmaicher force-pushed the fix-doctrine-eventlistener-prio branch from e47cf38 to 9d9d4ef Compare March 14, 2017 20:52
@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Mar 15, 2017
@chapterjason
Copy link
Contributor

chapterjason commented Mar 16, 2017

It does what I expect and I dont see anything missing.

Mentioning in the docs that priority and lazy are also available would be a good idea. Also to referring to the default event docs because it works in the same way?

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@fabpot
Copy link
Member

fabpot commented Mar 17, 2017

Thank you @dmaicher.

@fabpot fabpot merged commit 9d9d4ef into symfony:2.7 Mar 17, 2017
fabpot added a commit that referenced this pull request Mar 17, 2017
…s (dmaicher)

This PR was merged into the 2.7 branch.

Discussion
----------

[Doctrine Bridge] fix priority for doctrine event listeners

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21977
| License       | MIT
| Doc PR        | -

This fixes handling the priorities for doctrine event listeners. As found out by @chapterjason in #21977 the priority was incorrectly handled as soon as a listener had more than one tag (so listening to multiple events).

With this changes all tagged listeners are globally sorted by priority (using the same stable sort approach as in the later available `PriorityTaggedServiceTrait`) and then added one by one to the event manager.

I also updated the tests a bit as it was not covering all cases.

We also have to extend the docs for it I think as it does not mention the `priority` and `lazy` option at all? http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html

Commits
-------

9d9d4ef [Doctrine Bridge] fix priority for doctrine event listeners
@dmaicher dmaicher deleted the fix-doctrine-eventlistener-prio branch March 17, 2017 20:19
fabpot added a commit that referenced this pull request Mar 26, 2017
… suite (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Bridge\Doctrine] Fix change breaking doctrine-bundle test suite

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Doctrine Bundle's test suite [is currently broken](https://travis-ci.org/doctrine/DoctrineBundle/jobs/215222182) with `2.8@dev` because the tests expect `addEventListener` to be called with an array as first arg, but #22001 optimized them away as string. Since internally strings are turned back into arrays, let's tweak that change and make Doctrine Bundle green again.

Commits
-------

0577c7b [Bridge\Doctrine] Fix change breaking doctrine-bundle test suite
This was referenced Apr 4, 2017
@eXaminator
Copy link

Just a heads up: This change can have bc breaking side effects in code that (probably unknowingly) relied on the order of unprioritized subscribers (and probably listeners). This happened to us.

In addition to now recognizing priorities as expected this code also seems to change the order of unprioritized (or equally prioritized) subscribers (and probably listeners) compared to the old version.

@stof
Copy link
Member

stof commented Apr 26, 2017

@eXaminator previously, the order of services with equal priority (if you don't define it, it is 0) was totally impossible to know. The sorting was not stable, so PHP was free to return the order it wants (which might depend on the PHP version). They are now in the order of service definitions (i.e. something which cannot change magically)

@eXaminator
Copy link

@stof I understand that and I don't argue with it. But still, a PHP update could have changed and break things, this symfony update did change and break things (in our case). I just wanted to let everyone know that theses changes can have (and had) an impact on stuff besides just fixing a prior bug.

@stof
Copy link
Member

stof commented Apr 26, 2017

@eXaminator PHP does not even guarantee that the sorting of ex-aequo is the same on all OS running it (and maybe not on the same machine for 2 subsequent runs, even though this one is unlikely).
If you really have the need for a listener to run before or after another one, using priority is the only way to ensure it works fine.

@dmaicher
Copy link
Contributor Author

@eXaminator thanks for the info and sorry that it caused problems for you 😢

But as @stof said this is the only way we can actually guarantee a correct & stable sorting by priorities now. It was clear that with this patch the behavior will change compared to the previous version in certain scenarios that relied on the magic & unstable sorting from before.

@eXaminator
Copy link

Like I said, I'm not trying to argue against this patch. It makes sense and our code is at fault for breaking (or in this case also the DoctrineExtensionBundle to some degree, as Softdeleteable and Tree don't work together very well and our workaround for that stopped working now). But it was a pain to debug and I just wanted to let you guys know that it has potential bc breaking side effects on code that relies on the old (wrong) behavior (like having one subscriber clean up the unit of work before the next subscriber gets to it, as was the case here).

nicolas-grekas added a commit that referenced this pull request May 4, 2018
…s (dmaicher)

This PR was merged into the 3.4 branch.

Discussion
----------

[Doctrine Bridge] fix priority for doctrine event listeners

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21977
| License       | MIT
| Doc PR        | -

As discussed in #27126 this ports changes from #22001 to 3.4 that were dropped when merging 2.8 into 3.2 here: dc66960#diff-27d2e9b071d766df504c3fe4131e7abf

I took my original changeset from 2.8 and applied all commits since then on top of that.

Commits
-------

b3ac938 [Doctrine Bridge] fix priority for doctrine event listeners
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants