0

I have in my controller $id it's a foreign key

  $query = $em->getRepository('SurgeryPatientBundle:Patients')->findPatientByUserID($id);

And in my repository file this function

 public function findPatientByUserID($id)
{

    return $this->getEntityManager()
        ->createQuery('SELECT p FROM SurgeryPatientBundle:Patients p WHERE p.user ='.$id.'')
        ->execute();

}

I want get an instance of object but still get an array. Query with find($id) works good

edit Problem solves , I'm so stupid , I had invoked to $query[0]

2 Answers 2

1

You can use $query->getSingleResult(); as well

see here

http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#query-result-formats

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to grab the object, you shouldn't be using DQL. Doctrine entities have a find function that takes care of this for you.

Instead of all that code, you can just use (in your controller):

$em->getRepository('SurgeryPatientBundle:Patients')->find($id);

DQL is very powerful, but for simple lookups like this using the built in find methods will be more efficient & provide the entities as doctrine objects.

3 Comments

then findByUser should work if you pass a user instance i think.
It dosen't works I still get this array(1) { [0] => class Surgery\PatientBundle\Entity\Patients#914 (29) . When I find a primary key (find($id)) I get an object
findByUser($userObject) will return always an array. But inside your array is the Patients Object. If you need a function returning only the Patient, just wrap it into a own function returning the first element or null, but be carfull nobody ensures that your User has only one Patient. If a user can have more than one Patient it makes no sense to return one Object by querying a one to many relation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.