Skip to content

Instantly share code, notes, and snippets.

@drmmr763
Last active December 6, 2017 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmmr763/cfef8f1695989f5d0bf8b4b7a22f22d9 to your computer and use it in GitHub Desktop.
Save drmmr763/cfef8f1695989f5d0bf8b4b7a22f22d9 to your computer and use it in GitHub Desktop.
findOne vs DQL
<?php
/**
* Class TempRepository
*/
class TempRepository extends EntityRepository
{
/**
* Find a temp entity using the temps ID
*
* @throws NoResultException|NonUniqueResultException
* @param $tempID
* @return mixed
*/
public function findOneByTempID($tempID)
{
// debugger
// \Doctrine\Common\Util\Debug::dump($result);
// option 1: loads in 1.2 - 900ms on average
$qb = $this->createQueryBuilder('a');
$qb->select(array('t'))
->from($this->getEntityName(), 't')
->where('t.tempID = :tempID')
->setParameter('tempID', $tempID);
$result = $qb->getQuery()->getSingleResult();
return $result;
// option 2: loads in 200 - 500ms on average
return $this->findOneBy(['tempID' => $tempID]);
// option 3: loads in 300 - 800ms
/** @var Query $query */
$query = $em->createQuery("
SELECT t FROM App\Entities\Temps\Temp t
where t.tempID = :tempID
");
$query->setParameter('tempID', $tempID);
return $temp = $query->getSingleResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment