Search code examples
symfonyeventsdoctrine-ormtraceaddeventlistener

symfony2 doctrine2 : create an event that listens for select queries and returns the trace


With my symfony2/doctrine2 application, there are cases when I have a hard time figurint out where I did not addselect the right entities in my queries, which results in abnormal number of queries.

I am looking forward to build a class that would return the trace for each query I've launched, so I can understand where I need to addselect what.

Questions:

  1. is the postLoad event of doctrien the right one to do that ? I did not found a select event
  2. once I am in my eventListener, how can I get the trace from something even without an exception being thrown ?

EDIT :

I managed to modify the profiler. See how it's done here :

Symfony2 / Doctrine2 : How to override DebugStack class used in doctrine DataCollector?


Solution

  • If you are talking about SELECT queries than probably the postLoad event is enough.

    You can always use debug_backtrace to generate the trace without any exceptions. http://php.net/manual/en/function.debug-backtrace.php

    Then you can log it for further processing.