如何在本地查询COLUMNS上使用Doctrine2类型转换 [英] How To Utilize Doctrine2 Type Conversion on Native Query COLUMNS

查看:133
本文介绍了如何在本地查询COLUMNS上使用Doctrine2类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有找到任何东西 - 似乎应该是直截了当的。

Could not find anything on this-- seems like it should be straight forward though.

所以Doctrine2文档给出的例子对于绑定参数的类型转换如下所示:

So the example the Doctrine2 docs give for type conversion on bound parameters looks like this:

$date = new \DateTime("2011-03-05 14:00:21");
$stmt = $conn->prepare("SELECT * FROM articles WHERE publish_date > ?");
$stmt->bindValue(1, $date, "datetime");
$stmt->execute();

我想做的是指定其中一列的类型转换,但没有任何内容文件或StackOverflow我可以找到。一个伪代码可能是这样的:

What I want to do is specify the type conversion for one of the columns, but there is nothing in the documents or on StackOverflow that I could find. A pseudo-example of what this might look like:

$stmt = $conn -> prepare("SELECT datetime FROM articles WHERE id = 1");
$stmt -> setType(0, "date_type"); // 0 being the column position, "date_type" being the PHP type to convert to

如果有人知道如何做到这一点(这是SQL不是DQL),我会非常感激。谢谢。

If anybody knows how to do this, (this is SQL not DQL), I would greatly appreciate. Thank you.

推荐答案

这不是在DBAL级别工作的东西。如果您在ORM中使用NativeSQL查询,您可以通过水化获得这种转换(请参阅 HYDRATE_ARRAY 模式并映射某些内容,并将其中的某些内容映射到一起,然后在doctrine ORM文档中的nativeSQL部分中使用native-sql.html#scalar-resultsrel =noreferrer将获取的字段转换为实体。最快的解决方案(如果你不打算使用ORM)是通过访问 Doctrine\DBAL\Types\Type :: getType($ someType)来迭代结果并手动应用类型转换) - > convertToPhpValue($ fetchedValue)。它可能是ORM的一个有价值的补充,可以在 Doctrine\ORM\Query\ResultSetMapping#addScalarResult 中指定提取的类型的第三个参数。

This is not something that works at DBAL level. If you are using NativeSQL Queries in ORM, you can get that kind of conversion through hydration (see the NativeSQL section in the Doctrine ORM documentation) by using the HYDRATE_ARRAY mode and mapping some of the fetched fields to an entity. The fastest solution (if you don't intend to use ORM) is to iterate over the results and applying the type conversion manually by accessing Doctrine\DBAL\Types\Type::getType($someType)->convertToPhpValue($fetchedValue). It could be a valuable addition to ORM to be able to specify a third parameter stating the fetched type in Doctrine\ORM\Query\ResultSetMapping#addScalarResult.

这篇关于如何在本地查询COLUMNS上使用Doctrine2类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆