1 回答
TA貢獻1844條經驗 獲得超8個贊
最后我找到了解決方案。在每個包裝類中,我使用動態屬性來獲取關系集合,除了強加額外的查詢外,這會導致延遲加載。因此,在將模型集合傳遞給每個包裝器之前,檢索必要的關系模型,每個包裝器首先使用方法getRelations()(返回可用關系數組)檢查關系是否存在。如果預期關系可用,則將關系模型集合傳遞到適當的包裝類中。
用戶實體包裝器:
class UserEntityWrapper extends EntityWrapper
{
protected $entityClass = UserEntity::class;
protected function makeEntity($userEntity, $model)
{
$userEntity->setId($model->user_id);
$userEntity->setName($model->name);
// set other properties of user entity...
//--------------- relations -----------------
$relations = $model->getRelations();
$products = $relations['products'] ?? null;
if ($products) {
$userEntity->setProducts((new ProductEntityWrapper($products))->entity);
}
return $userEntity;
}
}
并且,類似的功能用于其他包裝器。
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報
