0

I have a doctrine query who returns the following result:

array(2) {
  [0]=>
  array(4) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(6) "Club 2"
    ["points"]=>
    int(19)
    ["played"]=>
    int(3)
  }
  [1]=>
  array(4) {
    ["id"]=>
    int(3)
    ["name"]=>
    string(6) "Club 3"
    ["points"]=>
    int(6)
    ["played"]=>
    int(1)
  }
}

Is it possible that $query->getResult() returns an object instead of an array ?

And do something like this:

foreach ( $clubs AS $oneClub ) {

    echo $oneClub->name;

}

Entity code:

public function getUserClubs ( $pUserId ) {

    $em = $this->getEntityManager();

    $query = $em->createQuery(

        '
       SELECT club.id, club.name, user_club.points, user_club.played
       FROM TennisBundle:UsersClubs user_club
         INNER JOIN TennisBundle:Clubs club WITH club.id = user_club.club
       WHERE user_club.user = :user
       ORDER BY user_club.points DESC
       '

    ) -> setParameters( array(

            'user' => $pUserId

    ));

    try {

            var_dump( $query->getResult() ); die;
            return (object) $query->getResult();

    } catch ( \Doctrine\ORM\NoResultException $e ) {


    }

}
Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
Paul
  • 1,290
  • 6
  • 24
  • 46

1 Answers1

0

This method should be placed in your "TennisBundle:UsersClubs" repository class:

public function getUserClubs ( $pUserId ) {

    $query= $this->createQueryBuilder('uc')
        ->join('inst.club', 'c')
        ->where('uc.user = :user')
        ->orderBy('uc.points', 'desc')
        ->setParameter('user', $pUserId)
        ->getQuery();

    $results = $query->getResult();
    $query = $em->createQuery(
}

or you can use "createQuery":

public function getUserClubs($pUserId) {
    $em = $this->getDoctrine()->getManager();

    $query = $em->createQuery(
            'SELECT uc FROM TennisBundle:UsersClubs uc
            JOIN TennisBundle:Clubs c
            WHERE uc.user = :user
            ORDER BY uc.points DESC'
        )->setParameter('user', $pUserId);

   $results = $query->getResult();
}

Parse object results and retrieve your club data. I hope this helps.

// $result is instance of "TennisBundle:UsersClubs"
foreach ( $results AS $result) {
    // $club is instance of "TennisBundle:Clubs"
    $club = $result->getClub();
    echo $club->getName();

}
Cristian Bujoreanu
  • 1,147
  • 10
  • 21
  • Ok thx, but I would like to keep using createQuery() instead of createQueryBuilder() – Paul Sep 14 '15 at 13:42