Symfony - calling a member function in an array

I have the following function:

public function getUserMediaAction(Request $request) { $data = $this->decodeToken($request); $username = $data['email']; $user = $this->getDoctrine()->getRepository('ApplicationSonataUserBundle:User') ->findOneBy(['email' => $username]); $idUser = $user->getId(); $media = $this->getDoctrine()->getRepository('ApplicationSonataUserBundle:User') ->findNewbieAllData($idUser); return new JsonResponse(['media' => $media->getPath()]); } 

findNewbieAllData:

 public function findNewbieAllData($id_newbie) { $sql = "SELECT m FROM ApplicationSonataUserBundle:User u JOIN ApplicationSonataUserBundle:Media m WHERE u.id=?1"; $query = $this->getEntityManager() ->createQuery($sql) ->setParameter(1, $id_newbie); $result = $query->getArrayResult(); return $result; } 

which unfortunately returns the following error:

 Call to a member function getPath() on array Stack Trace in src\CoolbirdBundle\Controller\UserController.php at line 97 - $media = $this->getDoctrine()->getRepository('ApplicationSonataUserBundle:User') ->findNewbieAllData($idUser); return new JsonResponse(['media' => $media->getPath()]); } 

Can anyone think how I can solve this problem please?

early

+5
source share
2 answers

Try instead:

 return new JsonResponse(['media' => $media[0]->getPath()]); 

Edit: Looking at the output of var_dump , you should try the following:

 return new JsonResponse(['media' => $media[0]['path']]); 
+2
source

The findNewbieAllData method findNewbieAllData data using arrays.

 $result = $query->getArrayResult(); return $result; 

Take a look at the getArrayResult method here:

https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/AbstractQuery.php

Use the getResult method getResult .

+1
source

Source: https://habr.com/ru/post/1272996/


All Articles