Doctrine 2.0 原生查询,无需映射
我正在编写一个很小的迁移脚本,我只想更新一个元素的一个属性。 我需要的结果在本地环境中没有表示形式,所以我需要的是一个非常简单的 SQL(这里是 Oracle)处理程序,我可以迭代它并获取返回的数组。
这在教义上可能吗?
即我想这样做:
$query = "SELECT t2.status FROM t2 LEFT JOIN t1 ON t1.id = t2.foreinkey";
$iterator = $connection->execute($query)->iterate();
foreach ($iterator as $array) {
// do something with an associative array
}
更新/解决方案: 根据 Corbin 的提示,我想出了这个解决方案,效果非常好:
$query = "SELECT t2.status FROM t2 LEFT JOIN t1 ON t1.id = t2.foreinkey";
$iterator = $connection->query($query);
while (is_object($iterator) AND ($array = $iterator->fetch()) !== FALSE) {
// do something with an associative array
}
I am writing a tiny little migration script and i am only trying to update one attribute of one element.
The Result i need has no Representation in the local Environment, so what i would need is a very simple SQL (here it is Oracle) handler that i can iterate over and get an array returned.
Is that possible with doctrine?
i.e. i would want to do this:
$query = "SELECT t2.status FROM t2 LEFT JOIN t1 ON t1.id = t2.foreinkey";
$iterator = $connection->execute($query)->iterate();
foreach ($iterator as $array) {
// do something with an associative array
}
UPDATE / SOLUTION:
With the Hint from Corbin i came up with this Solution which works pretty fine:
$query = "SELECT t2.status FROM t2 LEFT JOIN t1 ON t1.id = t2.foreinkey";
$iterator = $connection->query($query);
while (is_object($iterator) AND ($array = $iterator->fetch()) !== FALSE) {
// do something with an associative array
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
https:// www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html#native-sql
如果您想进行任何映射。
另一种选择是从 EntityManager::getConnection 获取连接对象并对其进行操作。
它返回一个 Doctrine\DBAL\Connection 您应该能够使用它。它有典型的fetchColumn、fetchArray、fetchAssoc等。
https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html#native-sql
If you want to do any mapping.
Another option would be to get the connection object from EntityManager::getConnection and operate on it.
It returns a Doctrine\DBAL\Connection which you should be able to work with. It has the typical fetchColumn fetchArray fetchAssoc so on.