Created
December 20, 2015 21:32
-
-
Save damour/c14844e747486e7e6223 to your computer and use it in GitHub Desktop.
doctrine custom hydrator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Lvlp\MapcontBundle\Command; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\Internal\Hydration\ArrayHydrator; | |
use Doctrine\ORM\Query; | |
use Doctrine\ORM\Query\Parser; | |
use Lvlp\MapcontBundle\Repository\PresetRepository; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class TestIterateCommand extends Command | |
{ | |
/** | |
* @var PresetRepository | |
*/ | |
private $repository; | |
/** | |
* @var EntityManager | |
*/ | |
private $entityManager; | |
/** | |
* @param PresetRepository $repository | |
* @param EntityManager $entityManager | |
*/ | |
public function __construct(PresetRepository $repository, EntityManager $entityManager) | |
{ | |
parent::__construct(); | |
$this->repository = $repository; | |
$this->entityManager = $entityManager; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configure() | |
{ | |
$this->setName('mapcont:test:iterate'); | |
} | |
public function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$queryBuilder = $this->repository->getIterate(); | |
$queryBuilder->orderBy('origin_type.id', 'ASC'); | |
$query = $queryBuilder->getQuery(); | |
$parser = new Parser($query); | |
$hydrator = new ArrayHydrator($this->entityManager); | |
$resultSetMapping = $parser->parse()->getResultSetMapping(); | |
foreach ($this->getResult($query) as $row) { | |
$stmt = new HydratorMockStatement($row); | |
$hydrator->hydrateAll($stmt, $resultSetMapping)[0]; | |
//echo json_encode(); | |
} | |
echo 'After ' . memory_get_usage(true) / 1024 / 1024 . PHP_EOL; | |
//var_dump($result); | |
} | |
private function getResult(Query $query) | |
{ | |
echo 'Before ' . memory_get_usage(true) / 1024 / 1024 . PHP_EOL; | |
$data = $this->entityManager->getConnection() | |
->executeQuery($query->getSQL(), $query->getParameters()->toArray()); | |
echo 'After ' . memory_get_usage(true) / 1024 / 1024 . PHP_EOL; | |
$rowResult = []; | |
$currentPrimaryKeyValue = null; | |
while (($result = $data->fetch()) || count($rowResult) > 0) { | |
if ($result === false) { | |
yield $rowResult; | |
break; | |
} | |
$primaryKeyValue = $result['id0']; | |
if ($currentPrimaryKeyValue !== null && $primaryKeyValue != $currentPrimaryKeyValue) { | |
yield $rowResult; | |
$rowResult = []; | |
} | |
$rowResult[] = $result; | |
$currentPrimaryKeyValue = $primaryKeyValue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment