Symfony2 / Doctrine make $statement->execute() not “buffer” all values

前端 未结 3 1426
滥情空心
滥情空心 2021-02-09 14:06

I\'ve got a basic codeset like this (inside a controller):

$sql = \'select * from someLargeTable limit 1000\';
$em = $this->getDoctrine()->getManager();
$c         


        
3条回答
  •  萌比男神i
    2021-02-09 14:27

    The selected answer is wrong and @kroky's answer should be selected as the correct one.

    The problem is Buffer vs Unbuffered Queries.

    Now it won't be a good idea to change the behaviour for all queries, because:

    Unless the full result set was fetched from the server no further queries can be sent over the same connection.

    Hence, it should only be used when necessary. Here is a full working example with >200k objects:

        $qb = ...->createQueryBuilder('p');
    
        $this
            ->em
            ->getConnection()
            ->getWrappedConnection()
            ->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
    
        $query = $qb->getQuery();
        $result = $query->iterate();
        $batchSize = 20;
        $i = 0;
        foreach ($result as $product)
        {
            $i++;
    
            var_dump($product[0]->getSku());
    
            if (($i % $batchSize) === 0) {
                $this->em->flush();
                $this->em->clear(); // Detaches all objects from Doctrine!
            }
        }
    

    It most likely needs some refinement.

提交回复
热议问题