我找到了这个答案 如何在 Symfony 中使用 getRoute 获取对象及其翻译?.但这是symfony 1.x的解决方案。
我使用注释进行路由,目前在symfony 3.1.9中,虽然这有效,但我想指定用于从数据库中加载id的URL对象的方法。原因是连接一些表,所以我的查询更少。
这可以在symfony 2/3中完成吗?
示例注释:
/**
* View the recipe with the given ID if it is available in the current
* locale.
*
* @param Request $request
* @param MyNameMyBundleEntityRecipe $object
* @return Response A Response instance
* @throws NotFoundHttpException
*
* @Route("/{id}", options={"method" = "ernst"})
* @Method({"GET"})
* @Breadcrumb("{object.name}")
*/
public function viewAction(Request $request, MyNameMyBundleEntityRecipe $object = null)
{
// do stuff
}
配方有自己的存储库:
/**
* Recipe
*
* @ORMTable()
* @ORMEntity(repositoryClass="MyNameMyBundleRepositoryRecipeRepository")
*/
class Recipe
{
}
gps_sflover给出的方向很好。控制器现在如下所示:
/**
* View the recipe with the given ID if it is available in the current
* locale.
*
* @param Request $request
* @param MyNameMyBundleEntityRecipe $object
* @return Response A Response instance
* @throws NotFoundHttpException
*
* @Route("/{id}")
* @ParamConverter("object", options={
* "repository_method" = "findForLocale",
* "mapping": {"_locale": "locale", "id": "id"},
* "map_method_signature" = true
* })
* @Method({"GET"})
* @Breadcrumb("{object.name}")
*/
public function viewAction(Request $request, MyNameMyBundleEntityRecipe $object)
{
// do stuff
}
配方存储库现在有这种方法:
class RecipeRepository extends EntityRepository
{
/**
* Return the recipe for the given id associated with the needed information
* to show on the recipe detail page. The current locale is set as a condition.
*
* @param string $locale
* @param int $id
* @return VaartiesDwehBundleEntityRecipe
*/
public function findForLocale($locale, $id)
{
$queryBuilder = $this->findAllForLocale($locale);
$queryBuilder
->andWhere('e.id = :id')
->setParameter('id', $id);
return $queryBuilder
->getQuery()
->getOneOrNullResult();
}
/**
* Find all objects of the current entity joined with all off its translations
* for the language in the given locale.
*
* TODO: See if the complete locale needs to be checked and the language
* only as a fallback. See https://github.com/KnpLabs/DoctrineBehaviors/issues/309
*
* @param string $locale
* @return QueryBuilder
*/
public function findAllForLocale($locale)
{
$language = $this->getLanguageFromLocale($locale);
$queryBuilder = $this->findAllJoinedToTranslation();
$queryBuilder
->where('t.locale = :locale')
->setParameter('locale', $language);
return $queryBuilder;
}
/**
* Find all objects of the current entity joined with all off its translations.
*
* @return QueryBuilder
*/
public function findAllJoinedToTranslation()
{
// A QueryBuilder is returned for use in the KnpPaginatorBundle for it being
// able to add sorting and filtering.
return $this->createQueryBuilder('e')
->select('e, t')
->join('e.translations', 't');
}
/**
* The language part from the locale is returned. If the locale doesn't contain
* an underscore, the whole locale is returned.
*
* @param string $locale
* @return string
*/
protected function getLanguageFromLocale($locale)
{
$localeParts = explode('_', $locale);
if (is_array($localeParts)) {
$language = reset($localeParts);
} else {
$language = $locale;
}
return $language;
}
}