这里有一些学说专家,他能解释一下,为什么这些DQL不会在Tally实体上初始化 tallyRevs 字段?我想,当我向他们提取TallyRevs(所有者方)和fetchJoin Tally实体时,该字段 tallyRevs 将被初始化。我究竟做错了什么?我需要通过DQL根据某些条件选择 TallyRev ,因为它是双向关联,我希望它也可以从其他(Tally.tallyRevs)方面初始化。
<?php
/**
* @ORM\Entity
* @ORM\Table(name="v3_overview_calloff_tally")
*/
class Tally
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var TallyRev[]|Collection
* @ORM\OneToMany(targetEntity="STI\Model\Entity\V3\Overview\CallOff\TallyRev", mappedBy="tally")
*/
private $tallyRevs;
}
/**
* @ORM\Entity
* @ORM\Table(name="v3_overview_calloff_tallyrev")
*/
class TallyRev
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
* @ORM\Column(type="integer", nullable=false)
*/
private $revision;
/**
* @var Tally
* @ORM\ManyToOne(targetEntity="STI\Model\Entity\V3\Overview\CallOff\Tally", inversedBy="tallyRevs")
* @ORM\JoinColumn(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)
;
答案 0 :(得分:0)
如果你想在获取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)