Determining which field calls Doctrine to query the database again

I use Doctrine with Symfony in several web application projects.

I optimized many queries in these projects to select only the fields that are needed in the database. But over time, new features were added, and in several cases additional fields are used in the code, forcing the lazy Doctrine loader to re-query the database and controlling the number of requests on some pages from 3 to 100+

Therefore, I need to update the original request to include all the required fields. However, it can’t be easy for Doctrine to register which field causes an extra request to be issued, so it becomes a daunting task to sift through code looking for fields that are not in the original request.

Is there a way to have a Doctrine log when the getter accesses a field that has not been hydrated?

+5
source share
2 answers

I did not have this problem, but I just looked at the Doctrine_Record class. Have you tried adding some debug output to the _get () method? I think in this part you should look for a solution:

    if (array_key_exists($fieldName, $this->_data)) {
        // check if the value is the Doctrine_Null object located in self::$_null)
        if ($this->_data[$fieldName] === self::$_null && $load) {
            $this->load();
        }
+1
source

SQL, . Doctrine 1.2 . post.

: , Doctrine_EventListener:

class QueryDebuggerListener extends Doctrine_EventListener
{
    protected $queries;

    public function preStmtExecute(Doctrine_Event $event)
    {   
        $query = $event->getQuery();
        $params = $event->getParams();

        //the below makes some naive assumptions about the queries being logged
        while (sizeof($params) > 0) {
            $param = array_shift($params); 

            if (!is_numeric($param)) {
                $param = sprintf("'%s'", $param);
            }   

            $query = substr_replace($query, $param, strpos($query, '?'), 1); 
        }   

        $this->queries[] = $query;
    }

    public function getQueries()
    {   
        return $this->queries;
    }
}

:

$c = Doctrine_Manager::connection($conn);
$queryDbg = new QueryDebuggerListener();
$c->addListener($queryDbg);
+1

All Articles