/**
* @param Channel $channel
* @param string $entity
*
* @throws \Exception
*/
protected function fillChannelToEntity(Channel $channel, $entity)
{
$interfaces = class_implements($entity) ?: [];
if (!in_array('OroCRM\\Bundle\\ChannelBundle\\Model\\ChannelAwareInterface', $interfaces)) {
return;
}
/** @var QueryBuilder $qb */
$qb = $this->em->getRepository($entity)->createQueryBuilder('e');
$iterator = new BufferedQueryResultIterator($qb);
$writeCount = 0;
$toWrite = [];
try {
$this->em->beginTransaction();
/** @var ChannelAwareInterface $data */
foreach ($iterator as $data) {
$writeCount++;
if (!$data->getDataChannel()) {
$channelReference = $this->em->getReference(ClassUtils::getClass($channel), $channel->getId());
$data->setDataChannel($channelReference);
$toWrite[] = $data;
}
if (0 === $writeCount % static::BATCH_SIZE) {
$this->write($toWrite);
$toWrite = [];
}
}
if (count($toWrite) > 0) {
$this->write($toWrite);
}
$this->em->commit();
} catch (\Exception $exception) {
$this->em->rollback();
throw $exception;
}
}