What is the preferred way to load one record with doctrine?
I am using
$em = EntityManager::create($connectionOptions, $config);
$dql = "select r from Rolle r";
$list = $em->createQuery($dql)->getResult();
to get a list of records.
I tried using
$dql = Doctrine_Query::create()
->from('Rolle r')
->where('r.ID = 14');
$rolle = $dql->fetchArray();
to get one record. Is this the preferred way?
If yes: I get the internal server error: Class 'Doctrine_Query' not found in ... on line ...
I searched for a file which contains the string 'Doctrine_Query', but I haven't such a file. So where do I find this class?
You might use getSingleResult()
instead of getResult()
. It's documentet in this chapter of the doctrine doc.
I think you're looking for getOneOrNullResult()
.
As scube answered, getSingleResult()
also works however this method enforces uniqueness of the result and will throw a NonUniqueResultException
if your query matches more than one record. It's almost a year since scube's answer though, it's possible that this was not the case at the time.
Credit: Cerad and m2mdas for their comment / answer here that lead me to getOneOrNullResult()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With