Symfony2 - 如何打印连接表输出

时间:2013-01-03 12:59:14

标签: symfony doctrine twig

我有两个联合表。使用print_r我会得到类似的内容

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*
                                )

                        )

我想要的是打印name:GameShelf\GamesBundle\Entity\Game:private] => Somebody,但我不知道如何。我使用Twig,我目前的模板是:

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

但它只从id表中输出Ownership

我的控制器:

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()
            ));
    }

回购:

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;
        }
    }

仅当我设置getSingleResult而不是getResult时才有效。

1 个答案:

答案 0 :(得分:0)

在Twig中,您使用相同的方法来访问实体信息。

[edit]查看递归后,您可能需要这样做:

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