Symfony Doctrine我如何找出何时/何时提取Lazy Relations

时间:2017-11-03 12:49:33

标签: php symfony doctrine-orm doctrine symfony-3.3

我正在开发一个使用分层关系的动作。 所以我有一个玩家实体,它有一个Relation OwnedCard。哪个有一个Relation CardLevel,它有一个Relation卡。 所以我正在使用

/**
 * @param Player $player
 * @Route("/{id}/cards", name="loki.tuo.ownedcard.cards.show", requirements={"id":"\d+"})
 *
 * @ParamConverter("player", class="LokiTuoResultBundle:Player", options={"repository_method" = "findWithOwnedCards"})
 * @return Response
 * @Security("is_granted('view.player', player)")
 */
public function showCardsForPlayerAction(Player $player)
{
    $allCards = $player->getOwnedCards();
    $allCards = Collection::make($allCards)->sortBy(function (OwnedCard $elem) {
        //$elem->getCard() calls the getName() method on CardLevel which delegates it to Card
        return $elem->getCard()->getName();
    });
    $deck = $allCards->filter(function (OwnedCard $item) {
        return $item->getAmountInDeck() > 0;
    });
    $combined = $deck->map(function (OwnedCard $item) {
        return $item->toDeckString();
    });
    $formOptions = ['attr' => ['class' => 'data-remote']];
    $ownedCardForm = $this->createForm(OwnedCardType::class, null, $formOptions);

    $massOwnedCardForm = $this->createForm(MassOwnedCardType::class, null, [
        'action' => $this->generateUrl('loki.tuo.ownedcard.card.add.mass', ['id' => $player->getId()]),
        'method' => 'POST',
    ]);
    //Render Template

为此,我创建了一个连接和选择这些关系的方法

public function findWithOwnedCards($id)
{
    $qb = $this->createQueryBuilder('player')
        ->join('player.ownedCards', 'ownedCards')
        ->join('ownedCards.card', 'cardLevel')
        ->join('cardLevel.card', 'card')
        ->addSelect(['ownedCards'])
        ->addSelect(['cardLevel'])
        ->addSelect(['card'])
        ->where('player.id = :id')
        ->setParameter('id', $id);
    return $qb->getQuery()->getSingleResult();
}

但不幸的是,Symfony Profiler告诉我,有很多像

这样的电话
SELECT * FROM card_level  WHERE card_id = ?

(我缩短了查询以提高可读性) 所以这意味着,在某些时候,Symfony / Doctrine不使用Joined Relationships,但不知何故认为它们是懒惰的,并且需要获取它们。

所以现在我的问题:我怎样才能找到,执行查询的地点或时间?代码中是否有一些我可以设置断点或抛出异常以查看堆栈跟踪以查看它来自何处?

1 个答案:

答案 0 :(得分:0)

尝试设置'获取'财产到" EAGER"为你的协会 这是一个来自学说文档的例子 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#manytoone