为长期问题道歉。
我有一个使用Doctrine的基于ZF3的应用程序。
我正在尝试将发送到MySQL服务器的“最终”查询记录到应用程序日志中。为此,我实施了一个基于this particular answer的解决方案。
注意:我知道ZF Debug Bar,但如果需要,我需要将查询记录到应用程序日志中以进行生产调试。
但是,在某些情况下,记录器会因致命错误而失败:
Unknown column type "2" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.
奇怪的是,错误只出现在记录器中 - 如果我禁用记录器,查询就会正常执行。
我的实现看起来像这样(我使用UUID作为主键):
'doctrine' => [
'configuration' => [
'orm_default' => [
'types' => [
UuidType::NAME => UuidType::class
],
'sql_logger' => DoctrineLog::class
]
],
]
class DoctrineLog extends DebugStack
{
public function startQuery($sql, array $params = null, array $types = null) {
parent::startQuery($sql, $params, $types);
// If there are no parameters set, just log the query as-is
if (empty($params)) {
Log::debug('Executing query: ', $sql, ';');
return;
}
$convertedSql = str_replace('?', '%s', $sql);
$convertedParams = [];
foreach ($params as $index => $param) {
$type = Type::getType($types[$index]);
$value = $type->convertToDatabaseValue($param, self::$dbPlatform);
$convertedParams[] = var_export($value, true);
}
Log::debug('Executing query: ', sprintf($convertedSql, ...$convertedParams), ';');
}
}
我调查了一下,我发现使用QueryBuilder
构建了一个在记录器中运行时失败的特定查询:
$queryBuilder->select('m')
->from(Mod::class, 'm')
->where('m.userId = :userId')
->orderBy('m.dateCreated', 'desc')
->setParameter('userId', $userId);
如果我使用findBy
存储库方法创建相同的查询,它会正确记录:
$entityManager->getRepository(Mod::class)->findBy(['userId' => $user->getId()], ['dateCreated' => 'desc']);
我已将其缩小到传递给$types
类中startQuery
方法的DoctrineLog
参数。
当查询正确记录时(使用findBy
存储库方法创建时),$types
数组如下所示:
[
0 => 'uuid'
]
当查询无法记录时(使用QueryBuilder
创建时),$types
数组如下所示:
[
0 => 2
]
我完全不知道2
应该是什么意思。就像我说的,奇怪的部分是查询成功执行,无论它是如何创建的。
虽然对于这个特定的查询我可以转储QueryBuilder
,但是在运行更新查询时也会发生这种失败,因此我需要查找原因或找到另一种记录整个查询的方法。
任何指针都会受到赞赏。
注意:types
返回的\Doctrine\DBAL\Types\Type::getTypesMap()
列表是:
[
[array] => Doctrine\DBAL\Types\ArrayType
[simple_array] => Doctrine\DBAL\Types\SimpleArrayType
[json_array] => Doctrine\DBAL\Types\JsonArrayType
[object] => Doctrine\DBAL\Types\ObjectType
[boolean] => Doctrine\DBAL\Types\BooleanType
[integer] => Doctrine\DBAL\Types\IntegerType
[smallint] => Doctrine\DBAL\Types\SmallIntType
[bigint] => Doctrine\DBAL\Types\BigIntType
[string] => Doctrine\DBAL\Types\StringType
[text] => Doctrine\DBAL\Types\TextType
[datetime] => Doctrine\DBAL\Types\DateTimeType
[datetimetz] => Doctrine\DBAL\Types\DateTimeTzType
[date] => Doctrine\DBAL\Types\DateType
[time] => Doctrine\DBAL\Types\TimeType
[decimal] => Doctrine\DBAL\Types\DecimalType
[float] => Doctrine\DBAL\Types\FloatType
[binary] => Doctrine\DBAL\Types\BinaryType
[blob] => Doctrine\DBAL\Types\BlobType
[guid] => Doctrine\DBAL\Types\GuidType
[uuid] => Ramsey\Uuid\Doctrine\UuidType
]