4
我读了一个很好的做法有实体光,只有getter和setters。但是,例如,我有一些逻辑的自定义方法。Symfony2:最好的地方把bussines逻辑
public function calculatePrice(){
/*
Here I have to do many math operations
including access to other repositories to get
specific data (taxes, comissions, ect) from others entities
*/
}
如果把这种“更复杂的方法”为办:
//in controller
$product->calculatePrice()
//in twig templates
{{ product.calculatePrice }}
但是使用服务类我不能在模板中调用$ product-> calculatePrice()。想象一下,你想打印一个简单的产品列表,以反映产品收集的相关价格。 ¿是否将$ this-> get('product.service') - > calculatePrice($ procduct)调用为foreach? ¿怎么样在{{product.calculatePrice}}中打印小枝模板中的计算价格? – smoreno 2012-07-13 22:55:41
我不使用Twig,所以我道歉如果我在这里错了,但不能'注入'服务类的实例,然后从循环内调用它的方法?是否可以通过Twig模板助手代理服务类? – 2012-07-13 22:57:31
是可能的扩展树枝并创建一个函数calculatePrice()。稍后,您可以执行以下操作:{{product | calculatePrice()}}。当然,我需要将负责计算价格的服务注入到树枝延伸课程中。谢谢。 – smoreno 2012-07-13 23:16:51