0

2つの結合されたテーブルがあります。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ます。

4

1 に答える 1

0

Twigでは、同じ方法を使用してエンティティ情報にアクセスします。

[編集]再帰を確認した後、次のようにする必要がある場合があります。

{% for owners in games %}
    {% for game in owners.getGame() %}
        {{ game.getId() }}
        {{ game.getName() }}
        {{ game.getParentId() }}
        {# etc... #}
    {% endfor %}
{% endfor %}
于 2013-01-03T13:27:00.167 に答える