Merge branch 'ticket-4059' into 2.5

Merge the changes from pull request #1638 into 2.5.

This set of changes combines the listeners from the global and local
event managers into one ordered set of listeners. This makes the
cognitive load lower around events, as there are no longer two separate
priority queues for the global and local managers.

Closes #2105
Improve readability and optimize minimizing variable lookups.
Only generate query data for hasOne and belongsTo associations.

Avoid calling generateAssociationQuery():

* when the return value will never be usefull (True in this case)
* to avoid polluting $queryData with uneeded fields

Later, the SQL statement for the primary, and 'hasOne' plus 'belongsTo'
relationships, is built.
Remove unnecessary call to getDataSource().
Move variables to logical blocks.
Unindent to ease readability, and avoid assigning variables when
unneeded.
Free a little memory before entering to recursive intensive loops.
Remove unneeded test inside conditional.
Remove unused $resultSet argument from generateAssociationQuery().
Remove inline assignment.
Fix a couple of failing tests.
Improve generateAssociationQuery():

* bail early when $linkModel is Null (BC for now).
* move SQL fields warmup and SQL statement building to its own functions
 (it will payoff later improving self documentation and readability).
* make assignments and function calls only when needed (depending on the
 association type).
Explicitly build SQL statements, calling buildAssociationQuery().
Unindent, and remove unnecessary getDataSource() call.
Also, objects should be CamelCase.
Remove the 'Line indented incorrectly' PHPCS error.
Remove reference from the signature.
Model objects variables should be CamelCase.
Assign  before actually using it.
Remove unused variable from the signature.
Remove 'self' key from getConstraint() calculations because it is not necessary anymore.
Model objects should be in CamelCase.
Call getDataSource() instead of the directly calling
ConnectionManager::getDataSource().