2012-02-02 118 views
0

我從學說1.2遷移到學說2.1時遇到用應該是什麼簡單的連接問題麻煩與Doctrine2連接表

這是我的第一個表:

/** 
* Model_WBS 
* 
* @Table(name="WBS") 
* @Entity 
*/ 
class Model_Wbs 
    /** 
    * @var integer $id 
    * 
    * @Column(name="id", type="integer", length=8) 
    * @Id 
    * @GeneratedValue(strategy="SEQUENCE") 
    * @SequenceGenerator(sequenceName="wbs_seq_seq", allocationSize=1, initialValue=1) 
    */ 
    public $id; 
    . 
    . 
    . 
    /** 
    * @var \Doctrine\Common\Collections\ArrayCollection 
    * 
    * @OneToMany(targetEntity="Model_WbsFund", mappedBy="id") 
    * @JoinColumn(name="id", referencedColumnName="wbs_id") 
    */ 
    public $wbsfunds; 

這是我第二次表

/** 
* Model_WbsFund 
* 
* @Table(name="WBS_FUNDS") 
* @Entity 
*/ 
class Model_WbsFund  
/** 
    * @var integer $id 
    * 
    * @Column(name="id", type="integer", length=8) 
    * @Id 
    * @GeneratedValue(strategy="SEQUENCE") 
    * @SequenceGenerator(sequenceName="wbs_funds_seq_seq", allocationSize=1, initialValue=1) 
    */ 
public $id; 

/** 
* @var integer $wbs_id 
* 
* @Column(name="wbs_id", type="integer", length=8) 
*/ 
public $wbs_id; 
. 
. 
. 
    /** 
    * @var \Doctrine\Common\Collections\ArrayCollection 
    * 
    * @ManyToOne(targetEntity="Model_Wbs", inversedBy="wbs_id") 
    * @JoinColumn(name="wbs_id", referencedColumnName="id") 
    */ 
    public $WBS; 

這裏是我的簡單測試

$testEntity = Model_Wbs::find(7185); 
foreach($testEntity->getwbsfunds() as $wbsfundobj){ 
    print("<br/>"); 
    print($wbsfundobj->FUND->getFundCode()." -- ".$wbsfundobj->WBS->getWbs()); 
} 

所以我期望看到與我第一次搜索的wbs相關聯的wbsfunds表中的基金代碼和wbs。 (我把 - > WBS-> getWbs()看看我是否真正獲得了與wbs id 7185相關的資金)。

取而代之,我得到所有wbs_funds記錄。我打印出來生成的查詢語句的學說,它是不正確

SELECT t0.id AS ID1, t0.wbs_id AS WBS_ID2, t0.fund_id AS FUND_ID3, t0.wbs_id AS WBS_ID4, t0.fund_id AS FUND_ID5 FROM WBS_FUNDS t0 

有趣的是,> wbsfundobj->賠償基金撥款─之間的聯繫geFundCode()的偉大工程,一樣的wbsfundobj-> WBS-> getWBS( ),但從WBS到WBS_FUNDS的鏈接看起來不對。

+0

喜歡受保護的屬性,而不是公共的。 http://stackoverflow.com/questions/4090609/how-can-public-fields-break-lazy-loading-in-doctrine-2 – Florian 2012-02-02 23:29:12

回答

0

通常,您可以在存儲庫上爲實體調用find()。例如:

// $em is your Entity Manager instance 
$testEntity = $em->getRepository('\Model_Wbs')->find(7185); 

// or a shorcut 
$testEntity = $em->find('\Model_Wbs', 7185); 

如何獲取對EntityManager的引用取決於您如何引導原理。如果您使用的Bisna application resource plugin,那麼它會是這樣(在控制器):

$em = $this->getInvokeArg('bootstrap')->getResource('doctrine')->getEntityManager(); 

通常情況下,我把這個到控制器的init()方法或寫一個動作助手,封裝上面,這樣我可以把它叫做步驟使用類似的東西:

$em = $this->_helper->doctrine->getEntityManager();