我使用Symfony2與Doctrine2。我要實現以下目標:來自EntityRepository的訪問會話
$place = $this->getDoctrine()->getRepository('TETestBundle:Place')->find($id);
而且在那個地方將是地方上的用戶語言(會話)的信息(通用數據+文本)。由於我要這麼做數百次,我想在幕後傳遞它,而不是作爲第二個參數。因此,英文用戶將以英文查看地點信息,用西班牙語查看西班牙語用戶。
一種可能性是從EntityRepository訪問應用程序的區域設置。我知道這是用服務和DI完成的,但我無法弄清楚它!
// PlaceRepository
class PlaceRepository extends EntityRepository
{
public function find($id)
{
// get locale somehow
$locale = $this->get('session')->getLocale();
// do a query with the locale in session
return $this->_em->createQuery(...);
}
}
你會怎麼做?你能否詳細解釋一下我要創建&的步驟和新課程?我打算一旦準備好就發佈這個翻譯包:)
謝謝!
你能解釋一下爲什麼你首先想要做到這一點嗎?在一個正常的MVC架構中,像locale這樣的東西將是Controller和View的責任,而不是Model。我想知道是否有更好的方法來做你想做的事情? –
@ tomtheman5剛剛更新了問題。謝謝! – fesja