Doctrine 2.0 原生查询,无需映射

发布于 2024-12-03 14:55:41 字数 702 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-12-10 14:55:41

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文