Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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