![](/img/trans.png)
[英]DDD Laravel. Repository pattern. How to retrieve an object from persistency and convert it into a not Laravel Entity model?
[英]Repository pattern in Laravel. Method from model
在使用存儲庫模式之前,我可以在對象模型上調用方法getImage()
class Product extends Eloquent{
public funtion getImage(){
//some query to database
}
}
因此,如果我將此類的對象傳遞給視圖,則可以在視圖中使用simple:
<img src="{{$product->getImage()}}"
但是,現在我實現了RepositoryPatter,所以我將getImage()移到了ProductRepository
class ProductRepository{
public function getImage(){
//some query to database
}
}
因此,如果我仍然想使用$ product-> getImage();該怎么辦? 在我看來
我考慮了幾種方法來解決此問題,具體取決於存儲庫返回的數據以及如何使用此方法。
1)如果您需要一種產品或一系列產品,並且都需要它們來獲取圖像,則可以在存儲庫中定義一個新方法:
getProductsWithImages()
{
//here you retrieve the needed products (that you already do) and retrieve and assign the images
}
因此,您只需在模板中執行以下操作:
<img src="{{ $product->image }}" ...
2)您的存儲庫可能返回您自己的類的對象(ProductEntity或類似的對象),並且它可以具有“ getImage”方法。
這樣,您當前的模板應該可以正常工作。
3)您可以使用另一個“幫助程序”類或服務來獲取所需的圖像:
<img src="{{ ProductHelper::getImage($image) }}" ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.