【问题标题】:Symfony - Doctrine getResult() VS toIterable()Symfony - 原则 getResult() VS toIterable()
【发布时间】:2022-11-11 21:05:19
【问题描述】:

我正在使用 Symfony 4.4 和 PHP 7.4。我的学说版本是 2.7。

我想遍历我的数据库文档。当我使用toIterable 时,我的内存每行都会增加。大约 15 分钟后,我遇到了 memory_limit 错误。

$documents = $this->em->getRepository(Document::class)->getDocuments($spool)->getQuery()->toIterable();
foreach ($documents as $document) {
     dump(Helper::formatMemory(memory_get_usage()));
}

当我使用getResult 时,我在每一行都有完全相同的内存。我想知道我的 getResult 如何比迭代器消耗更少的内存。

谢谢

【问题讨论】:

  • 你能添加教义的版本吗?
  • 我添加了版本号,是2.7。
  • 在 2.8.2 之前似乎有一个bug,其中 toIterable 没有释放对象内存
  • 你有时会清除你的实体经理($em->clear())吗?否则刷新的实体仍然会被跟踪,你会得到性能/内存问题
  • 是的,我正在使用$em->clear() 并使用模数刷新。

标签: php symfony doctrine-orm doctrine


【解决方案1】:

我目前无法更改 Doctrine 的版本。

我找到了解决方法:

public function getDocuments(Deployment $deployment): Traversable
    {
        $conn = $this->getEntityManager()->getConnection();

        $sql = '
            SELECT d.id
            FROM document d
            WHERE d.deployment_id = :deployment_id
            ';

        $stmt = $conn->prepare($sql)->executeQuery([
            'deployment_id' => $deployment->getId(),
        ]);

        return $stmt->iterateAssociative();
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多