Last active
December 6, 2017 16:42
-
-
Save drmmr763/cfef8f1695989f5d0bf8b4b7a22f22d9 to your computer and use it in GitHub Desktop.
findOne vs DQL
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 | |
/** | |
* 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