Fix for issue #6116.

Fix for issue #6116.
Currently using an advanced where inside a GlobalScope apply() method creates an enless loop because of the `->applyGlobalScopes()->removeGlobalScopes()` produced with `newQueryWithoutScopes()`.

I've moved some code to prevent this. Nothing new is added, just made sure of calling `->applyGlobalScopes()` only when needed...