[英]Doctrine ParamConverter using info of related Entities
I have in Symfony a Entity of Post
. 我在Symfony中有一个
Post
实体。 That project has translations, stored in PostTranslation
. 该项目的译文存储在
PostTranslation
。
Each Post has a slug that is different for each language. 每个帖子都有一种随语言而异的信息。
The code looks looks like: 该代码看起来像:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
/**
* @I18nDoctrine
* @Route("/blog/{slug}")
* @ParamConverter("post", class="SensioBlogBundle:Post")
*/
public function showAction(Post $post)
{
}
But how can I do a findOneBy('slug')
kind of search? 但是,如何进行
findOneBy('slug')
搜索? Because it is stored in the related Entity. 因为它存储在相关的实体中。
I guess besides slug
property your PostTranslation
entity stores some sort of language/locale, eg locale
property, am I right? 我想,除了
slug
财产的PostTranslation
实体保存某种语言/区域设置,例如locale
属性,是吗? I also guess that your website is interlocalized based on some parameter stored in URL (different (sub)domain, query string parameter, path prefix - it's irrelevant), right? 我还猜测您的网站是根据URL中存储的某些参数(不同的(子)域,查询字符串参数,路径前缀-无关紧要)进行了本地化,对吗?
There are at least three solutions that solve your problem: 至少有三种解决方案可以解决您的问题:
ParamConverter
and do everything by yourself. ParamConverter
并ParamConverter
完成所有操作。 Override DoctrineParamConverter
from SensionFrameworkExtraBundle and add support for finding by slug
and locale
for Post
class. 覆盖
DoctrineParamConverter
从SensionFrameworkExtraBundle并添加了对通过寻找支持slug
和locale
的Post
类。 I would also suggest creating a little bit more robust solution: 我还建议创建一个更强大的解决方案:
Translatable
interface that defines getTranslations()
and setTranslations()
methods and apply it to your Post
class. getTranslations()
和setTranslations()
方法的Translatable
接口,并将其应用于Post
类。 CustomDoctrineParamConverter
check whether requested class implements this interface, if so, it means that you're going to search entity based on its translation, that is locale
and some locale-unique property. CustomDoctrineParamConverter
检查所请求的类是否实现了此接口,如果是,则意味着您将基于其翻译(即locale
和某些语言环境唯一的属性)搜索实体。 PostTranslation
class that will define a single method, something like getLocaleIdentifier/UniqueProperty
that for PostTranslation
will return "slug"
. PostTranslation
类创建另一个接口,该接口将定义一个方法,类似于getLocaleIdentifier/UniqueProperty
类的方法, PostTranslation
将返回"slug"
。 Post
property, based on locale
and slug
property from its translations
assocation you can quite easily create DQL query to do that. translations
关联中的locale
和slug
属性找到一个Post
属性,则可以很容易地创建DQL查询来做到这一点。 ParamConverter
annotation has repository_method
property that allows you to use custom method from PostRepository
which should retrieve Post
entity. ParamConverter
注释具有repository_method
属性,该属性使您可以使用PostRepository
中的自定义方法,该方法应检索Post
实体。 The only problem is that built-in DoctrineParamConverter
will pass only slug
parameter to this method. 唯一的问题是内置的
DoctrineParamConverter
仅将slug
参数传递给此方法。 Your repository will have to informed about current locale in some way. 您的存储库将必须以某种方式通知当前语言环境。 This could be achieved in many different ways, here's one of them:
这可以通过许多不同的方式来实现,这是其中一种:
LocaleAware
interface with a single method setLocale($locale)
. setLocale($locale)
创建LocaleAware
接口。 PostRepository
PostRepository
实现该接口 Create event listener that listens for kernel.request
event: 创建监听
kernel.request
事件的事件监听器 :
LocaleAware
interface. LocaleAware
接口。 _locale
attribute into repository. _locale
属性注入存储库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.