史京迪
史京迪

Reputation: 481

Batch process in symfony2

I create a custom console command in symfony2. This command will call the api to find fit jobs for all users. then I'll get a error like this. My code is like:

 $em = $this->getContainer()->get('doctrine')->getEntityManager('default');
            $user = $em->getRepository('EnsUserBundle:User')->findAll();
                foreach ($user as $user) 
                {
                    .....
                    $em->flush();
                }
            }
        }

enter image description here

Then I want to clear the entitymanager for each user.

$em = $this->getContainer()->get('doctrine')->getEntityManager('default');
                $user = $em->getRepository('EnsUserBundle:User')->findAll();
                    foreach ($user as $user) 
                    {
                        .....
                        $em->flush();
                        $em->clear();
                    }
                }
            }

It will lead to a error: enter image description here Then i try to use the batch process:

$em = $this->getContainer()->get('doctrine')->getEntityManager('default');
        $user = $em->getRepository('EnsUserBundle:User')->findAll();
        $iterableResult = $em->getRepository('EnsUserBundle:User')->findAll()->iterate();
        while (($row = $iterableResult->next()) !== false) 
        {
            foreach ($user as $user) 
            {
                .....
                $em->flush();
                $em->clear();
            }
            $em->detach($row[0]);
        }
    }

But it leads to an error enter image description here

So, do you have any good advices? Thank you !

Upvotes: 0

Views: 2281

Answers (1)

sf_tristanb
sf_tristanb

Reputation: 8855

You could try :

  • Increase the maximum memory a script consumes inside php : php.ini memory_limit = 512M

  • Flush your objects when it reaches XX items (ex: each 100 items). You can clear your manager each time you flush it.

Upvotes: 1

Related Questions