Hi,
The following code below is an update method that belongs to a batch
process. It iterates over the Supplier entity and fetches 10 supplier ids
from the database per batch.
This code works okay, but on a variable moment the output of
getArrayResult() keeps returning zero items.
If I start the batch script again it starts fetching new items. I can't
figure out what the problem is here?! I get no errors or exceptions, just
zero items.
public function update()
{
$this->console->writeLine(sprintf('Started update - %s',
date('Y-m-d H:i:s')), Color::YELLOW);
if ($this->debug) {
$this->console->writeLine(
sprintf('(memory usage: ' . round(memory_get_usage(true) /
1024 / 1024, 2) . ' MB)')
);
}
$offset = 0;
$limit = 10;
$failCount = 0;
do {
$supplierIds = array();
$oneWeekAgo = new \DateTime();
$oneWeekAgo->sub(new \DateInterval('P7D'));
$suppliers = $this->entityManager->createQueryBuilder()
->select('s.id')
->from('Application\Entity\Supplier', 's')
->innerJoin('s.websites', 'w', Join::WITH, 'w.isMain =
true')
->where('s.isMember = false')
->andWhere('s.profileUpdated IS NULL OR s.profileUpdated <
:oneWeekAgo')
->setParameter('oneWeekAgo', $oneWeekAgo)
->orderBy('s.profileUpdated', 'ASC')
->setFirstResult($offset)
->setMaxResults($limit)
->getQuery()->getArrayResult()
;
foreach ($suppliers as $supplier) {
$supplierIds[] = $supplier['id'];
}
$supplierIdsCount = count($supplierIds);
if ($supplierIdsCount > 0) {
$suppliers = $this->entityManager->createQueryBuilder()
->select('s')
->from($this->entityManager->resolveEntity(Entity::SUPPLIER), 's')
->where('s.id IN
(:supplierIds)')->setParameter('supplierIds', $supplierIds)
->getQuery()->getResult()
;
foreach ($suppliers as $supplier) {
$this->updateProfile($supplier);
}
$this->entityManager->flush();
$this->entityManager->clear();
$this->console->writeLine('End of batch - supplier count in
this batch "' . $supplierIdsCount . '"',
Color::YELLOW);
sleep(2);
$failCount = 0;
$offset += $limit;
} elseif ($supplierIdsCount === 0 && $failCount < 3) {
$this->console->writeLine('End of batch - supplier count in
this batch "' . $supplierIdsCount . '"'
. ' - trying again...', Color::YELLOW);
$failCount++;
$supplierIdsCount = $limit;
}
if ($this->debug) {
$this->console->writeLine(
sprintf('(memory usage: ' .
round(memory_get_usage(true) / 1024 / 1024, 2) . ' MB)')
);
}
} while ($supplierIdsCount == $limit);
$this->console->writeLine(sprintf('Finished import - %s',
date('Y-m-d H:i:s')), Color::YELLOW);
}
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.