原则 2,DQL,关联未初始化



这里有一些学说专家,谁可以解释我,为什么这些 DQL 不会初始化 Tally 实体上的 TallyRevs 字段?我想,当我获取 TallyRevs(所有者端(并将加入 Tally 实体获取给他们时,该字段 TallyRevs 将被初始化。我做错了什么?我需要通过 DQL 根据某些标准选择 TallyRev,并且由于它是双向关联,我希望它也从另一端 (Tally.tallyRevs( 初始化。

转储屏幕

<?php

/**
 * @ORMEntity
 * @ORMTable(name="v3_overview_calloff_tally")
 */
class Tally
{
    /**
     * @var int
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var TallyRev[]|Collection
     * @ORMOneToMany(targetEntity="STIModelEntityV3OverviewCallOffTallyRev", mappedBy="tally")
     */
    private $tallyRevs;
}

/**
 * @ORMEntity
 * @ORMTable(name="v3_overview_calloff_tallyrev")
 */
class TallyRev
{
    /**
     * @var int
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var int
     * @ORMColumn(type="integer", nullable=false)
     */
    private $revision;
    /**
     * @var Tally
     * @ORMManyToOne(targetEntity="STIModelEntityV3OverviewCallOffTally", inversedBy="tallyRevs")
     * @ORMJoinColumn(name="tally_id", referencedColumnName="id", nullable=false)
     */
    private $tally;
}

下面是一些存储库代码:

$qb = $repository->createQueryBuilder();
$qb
    ->select('tallyRev')
    ->from(TallyRev::class, 'tallyRev', 'tallyRev.id')
    // complicated filtering, this is just an example
    ->andWhere($qb->expr()->in('tallyRev.revision', ':rev'))
    ->setParameter('rev', $rev)
;
$tallyRevs = $qb->getQuery()->getResult();
$ids = array_keys($tallyRevs);
$qb2 = $repository->createQueryBuilder();
$qb2
    ->select('partial tallyRev.{id}')
    ->from(TallyRev::class, 'tallyRev', 'tallyRev.id')
    ->andWhere($qb2->expr()->in('tallyRev.id', ':ids'))
    ->setParameter('ids', $ids)
    ->leftJoin('tallyRev.tally', 'tally')
    ->addSelect('tally')
;
$qb2->getQuery()->getResult();

我知道,我可以像这样从 Tally 端编写 DQL:

$qb
    ->select('tally')
    ->from(Tally::class, 'tally', 'tally.id')
    ->leftJoin('tally.tallyRev', 'tallyRev')
    ->addSelect('tallyRev')
    ->andWhere($qb->expr()->in('tallyRev.revision', ':rev'))
    ->setParameter('rev', $revs)
;
如果你想在

获取 tallyRev 时获取连接计数,你应该在第一个 qb 中写这样的东西,然后删除 qb2

->select(['tallyRev', 'tally'])
->from(TallyRev::class, 'tallyRev', 'tallyRev.id')
->join('tallyRev.tally', 'tally')
// complicated filtering, this is just an example
->andWhere($qb->expr()->in('tallyRev.revision', ':rev'))
->setParameter('rev', $rev)

;

相关内容

  • 没有找到相关文章

最新更新