Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony - Call to a member function on array

Tags:

php

symfony

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

like image 413
Dax Avatar asked Oct 30 '17 11:10

Dax


2 Answers

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']]);
like image 91
mega6382 Avatar answered Oct 17 '22 18:10

mega6382


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.

like image 1
John Smith Avatar answered Oct 17 '22 19:10

John Smith