Symfony2 - how to print joined tables output

2019-08-22 02:14发布

问题:

I have two joined tables. With print_r I am getting something like this

Array
(
    [0] => GameShelf\UsersBundle\Entity\Ownership Object
        (
            [id:GameShelf\UsersBundle\Entity\Ownership:private] => 1
            [type:GameShelf\UsersBundle\Entity\Ownership:private] => 1
            [time:GameShelf\UsersBundle\Entity\Ownership:private] => 2010-02-05 11:00:00
            [game:GameShelf\UsersBundle\Entity\Ownership:private] => Doctrine\ORM\PersistentCollection Object
                (
                    [snapshot:Doctrine\ORM\PersistentCollection:private] => Array
                        (
                            [0] => GameShelf\GamesBundle\Entity\Game Object
                                (
                                    [id:GameShelf\GamesBundle\Entity\Game:private] => 1
                                    [parent_id:GameShelf\GamesBundle\Entity\Game:private] => 0
                                    [name:GameShelf\GamesBundle\Entity\Game:private] => Somebody
                                    [slug:GameShelf\GamesBundle\Entity\Game:private] => somebody
                                    [reldate:GameShelf\GamesBundle\Entity\Game:private] => 2010-10-10
                                    [genres:GameShelf\GamesBundle\Entity\Game:private] => 1,2
                                    [platforms:GameShelf\GamesBundle\Entity\Game:private] => 1,2
                                    [developers:GameShelf\GamesBundle\Entity\Game:private] => 1,2
                                    [description:GameShelf\GamesBundle\Entity\Game:private] => Lipsum
                                    [desc_src:GameShelf\GamesBundle\Entity\Game:private] => http://onet.pl
                                    [rate:GameShelf\GamesBundle\Entity\Game:private] => 0
                                    [ownership:GameShelf\GamesBundle\Entity\Game:private] => GameShelf\UsersBundle\Entity\Ownership Object
 *RECURSION*
                                )

                        )

What I want, is to print name:GameShelf\GamesBundle\Entity\Game:private] => Somebody, but I don't know how. I use Twig, my current template is:

{% for game in games %}
{{ game.id }}
{% endfor %}

But it outputs only id from Ownership table.

My controller:

public function getOwnedAction($type = 1, $user = 1) {
        $games = $this->getDoctrine()
            ->getRepository('GameShelfUsersBundle:Ownership')
            ->getOwned();
        echo '<pre>';
        //print_r($games);
        echo '</pre>';

        return $this->render('GameShelfUsersBundle:Default:index.html.twig',
            array(
                'games' => $games->getGame()
            ));
    }

Repo:

public function getOwned($type = 1, $user = 1) {
        $query = $this->getEntityManager()
            ->createQuery('
                SELECT o, g FROM GameShelfUsersBundle:Ownership o
                JOIN o.game g
                WHERE o.type = :type
            ')
            ->setParameter('type',$type);

        try {
            return $query->getResult();
        } catch (\Doctrine\ORM\NoResultException $e) {
            return null;
        }
    }

It works only when I set getSingleResult instead of getResult.

回答1:

In Twig you use the same methods to access Entity information.

[edit] After looking at the recursion, you might need to do it this way:

{% for owners in games %}
    {% for game in owners.getGame() %}
        {{ game.getId() }}
        {{ game.getName() }}
        {{ game.getParentId() }}
        {# etc... #}
    {% endfor %}
{% endfor %}