我在我的應用程序 - 一個有點複雜的定價機制,這裏有一些我的業務規則設定階段(實體被加粗):Symfony2/Doctrine,必須在我的控制器中放入業務邏輯?和複製控制器?
- 一個產品可能有獨特的價位爲給定客戶,網站或客戶組。
- 一個產品有時可以有一個或多個附加選項可能有自己的價位或價格規則。
- A 產品有一個唯一加法由用戶選擇,這實質上是一個價格和一個整數。
現在,我有價位的EntityRepository基本上確定基本產品正確的價格點。 唯一加法和選項也是如此。
PricePointRepository
public function getThePrice($Product, $qty, $Website, $Customer = null)
{
//all logic to get product price for this given instance goes here. Good.
}
控制器(簡體)
public function indexAction()
{
$Product = $em->dostuffwithpostdata;
$qty = POST['qty']; //inb4insecure trolls
$Website = $em->dostuff();
$Customer = (if user is logged in, return their object with $em, otherwise null as it is a guest or public person); // No business logic here, just understanding the request.
$price = $em->getRepository(PricePointRepository)->getThePrice($Product,$qty,Website,$Customer);
$Options[] = $em->dostuffwithPOSTdata;
$optionsPrice = 0;
//Below is some logic directly related to pricing the product.
foreach($Options as $option) {
if($option->hasRule()) {
$optionsPrice += $ruleprice; //after some other stuff of course)
} else {
$optionsPrice += $em->getRepository(OptionPricePoints)->getPrice($option->getID(),$qty);
}
}
$uniqueAdditionPrice = $em->stuff;
$finalprice = $price + $optionsPrice + $uniqueAdditionPrice; //This is logic related to how I price this type of product!
$unitprice = $finalprice/$qty;
//twig stuff to render and show $finalprice, $unitprice, $uniqueAdditionPrice
}
這只是該產品的頁面。當這個邏輯需要重新使用時,當我到購物車,保存訂單等等會發生什麼。如您所見,我始終使用Doctrine根據存儲庫類中的業務邏輯提取數據。
我很高興地歡迎urdoingitwrong的答案,因爲我真的認爲這是錯誤的。我該如何解決這個問題?美好的東西是基本上是這樣一種服務:
$pricer = getPricerService->Pricer($Entities,$postdata,$etc);
$unitPrice = $pricer->getUnitPrice();
$totalPrice = $pricer->getTotalPrice();
$optionsPrice = $pricer->getOptionsPrice();
但我不知道如何去這樣做的Symfony /教義裏面,特別是教義和庫在控制器進行訪問的方式。
太棒了 - 謝謝!在你的迴應和庫巴之間,我已經掌握瞭如何完成這部分應用。 – Nick
如果您想在同一事務下合併兩個服務調用,該怎麼辦? – GorillaApe