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()]);
}
Would anyone have any idea of how I can solve this problem, please?
thank you in advance
Try the following instead:
return new JsonResponse(['media' => $media[0]->getPath()]);
Edit: Looking at the var_dump
output you should try this:
return new JsonResponse(['media' => $media[0]['path']]);
Method findNewbieAllData
hydrates data with arrays.
$result = $query->getArrayResult();
return $result;
Look at getArrayResult
method here:
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/AbstractQuery.php
Use getResult
method instead.
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