Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load one record with doctrine

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?

like image 428
user1027167 Avatar asked Nov 28 '22 09:11

user1027167


2 Answers

You might use getSingleResult() instead of getResult(). It's documentet in this chapter of the doctrine doc.

like image 116
scube Avatar answered Dec 06 '22 09:12

scube


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().

like image 34
WildWorx Avatar answered Dec 06 '22 07:12

WildWorx