2017-05-17 59 views
1

在這裏是一些學說專家,誰能解釋我,爲什麼這些DQLs不會初始化tallyRevs字段上的Tally實體?我認爲,當我獲取TallyRevs(所有者方)並獲取Tally實體給他們時,該字段tallyRevs將被初始化。我究竟做錯了什麼?我需要通過DQL選擇TallyRev,並且由於它是雙向關聯,所以我希望它也可以從其他(Tally.tallyRevs)端進行初始化。Doctrine2,DQL,協會未初始化

Screen of dump

<?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(); 

我知道,我可以從理貨側這樣寫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

如果你想當你提取tallyRev時,你應該在第一個qb和de中寫下類似的東西來獲取連接記錄lete 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) 

;