我有一組規(guī)范化的實(shí)體(偽代碼):User (id, name)Subscription (id, user_id, magazine_id, internal_reference)Magazine (id, name)通常,以標(biāo)準(zhǔn)原則方式創(chuàng)建新訂閱會(huì)很容易:// The internal reference is just to emphasize that I have logic inside the constructor that sets an important value$subscription = new Subscription($textThatWillBeMangledToGenerateInternalReference);$subscription->setMagazine($magazine);$subscription->setUser($user);$this->_em->persist($subscription);$this->_em->flush();然而,在我的用例中,我目前有一個(gè)實(shí)體,但只有1000個(gè)UserID。Magazine可能的解決方案:運(yùn)行類似 .這將是一個(gè)相當(dāng)可怕的解決方法,因?yàn)槲覍?duì)這些數(shù)據(jù)沒有欲望,并且它對(duì)將要觸發(fā)的查詢沒有任何影響。SELECT * FROM User WHERE id IN (:userIds)->persist創(chuàng)建某種偽實(shí)體?也許像那樣使用它。如果這有效(我不確定),這將是可怕的,因?yàn)閁serID是一個(gè)生成值,所以為它添加任何形式的外部設(shè)置器將再次成為一個(gè)相當(dāng)可怕的黑客$user = new User($userId);完全跳過學(xué)說實(shí)體系統(tǒng),并通過PDO將數(shù)據(jù)戳入。這樣做的好處是避免了教義逐個(gè)系統(tǒng)相對(duì)緩慢的性質(zhì),但這意味著我將跳過訂閱實(shí)體內(nèi)部的邏輯。這當(dāng)然可以移動(dòng)到其他地方,但我想嘗試并了解是否有一種慣用的方法可以做到這一點(diǎn)。INSERT$internalReference
1 回答
GCT1015
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
在尷尬的短時(shí)間之后,我在這里找到了我想要的答案。
您可以使用實(shí)體管理器創(chuàng)建部分引用,以實(shí)質(zhì)上允許(相對(duì))干凈的方式執(zhí)行方法 2
在我的問題的上下文中的示例:
$subscription = new Subscription($textThatWillBeMangledToGenerateInternalReference);
$subscription->setMagazine($magazine);
$subscription->setUser($this->_em->getPartialReference(User::class, array('id' => $userId)););
$this->_em->persist($subscription);
$this->_em->flush();
- 1 回答
- 0 關(guān)注
- 122 瀏覽
添加回答
舉報(bào)
0/150
提交
取消
