我在几个Web应用程序项目中将Doctrine与Symfony一起使用。
我优化了这些项目中的许多查询,以便仅从数据库中选择所需的字段。但是随着时间的推移,添加了新功能,并且在一些情况下 - 代码中使用了其他字段,导致 Doctrine 惰性加载器重新查询数据库,并将某些页面上的查询数量从 3 增加到 100+
因此,我需要更新原始查询以包含所有必填字段。但是,Doctrine 似乎没有一种简单的方法来记录哪个字段导致发出额外的查询 - 因此筛选代码以查找原始查询中没有的字段的用法是一项艰苦的工作。
有没有办法让 Doctrine 在 getter 访问未水合的字段时记录?
我没有这个问题,只是看了Doctrine_Record类。您是否尝试过向 _get() 方法添加一些调试输出?我认为这部分是您应该寻找解决方案的地方:
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();
}
只需打开SQL日志记录,您就可以从别名中推断出有罪的人。有关如何在教义 1.2 中做到这一点,请参阅这篇文章。
基本上:创建一个扩展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);