Symfony Doctrine 我如何找出懒惰关系的获取地点/时间



我正在研究一个使用分层关系的动作。所以我有一个玩家实体,它有一个关系拥有卡。它有一个关系卡级别,它有一个关系卡。所以我正在使用

/**
 * @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不使用连接关系,但不知何故认为它们是懒惰加载的,需要获取它们。

所以现在我的问题:我如何知道查询在何时何地执行?代码中是否有某个点可以设置断点或抛出异常以查看堆栈跟踪以查看其来源?

尝试将"fetch"属性设置为"EAGER"为您的关联这是教义文档中的示例http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#manytoone

相关内容

  • 没有找到相关文章

最新更新