简体   繁体   English

使用相关实体的信息的教义ParamConverter

[英]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: 至少有三种解决方案可以解决您的问题:

  1. Abandon the use of ParamConverter and do everything by yourself. 放弃使用ParamConverterParamConverter完成所有操作。
  2. Override DoctrineParamConverter from SensionFrameworkExtraBundle and add support for finding by slug and locale for Post class. 覆盖DoctrineParamConverterSensionFrameworkExtraBundle并添加了对通过寻找支持sluglocalePost类。 I would also suggest creating a little bit more robust solution: 我还建议创建一个更强大的解决方案:

    1. Create Translatable interface that defines getTranslations() and setTranslations() methods and apply it to your Post class. 创建定义了getTranslations()setTranslations()方法的Translatable接口,并将其应用于Post类。
    2. In your 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和某些语言环境唯一的属性)搜索实体。
    3. Now you need some way, to get the name of this locale-unique property. 现在,您需要某种方式来获取此区域唯一属性的名称。 You could create another interface for your PostTranslation class that will define a single method, something like getLocaleIdentifier/UniqueProperty that for PostTranslation will return "slug" . 您可以为PostTranslation类创建另一个接口,该接口将定义一个方法,类似于getLocaleIdentifier/UniqueProperty类的方法, PostTranslation将返回"slug"
    4. If you know, that you need to find a Post property, based on locale and slug property from its translations assocation you can quite easily create DQL query to do that. 如果您知道,您需要根据其translations关联中的localeslug属性找到一个Post属性,则可以很容易地创建DQL查询来做到这一点。
  3. 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: 这可以通过许多不同的方式来实现,这是其中一种:

    1. Create LocaleAware interface with a single method setLocale($locale) . 使用单个方法setLocale($locale)创建LocaleAware接口。
    2. Implement that interface in PostRepository PostRepository实现该接口
    3. Create event listener that listens for kernel.request event: 创建监听kernel.request事件的事件监听器

      1. Iterate through all Doctrine's repositories. 遍历所有Doctrine的存储库。
      2. Check if given repository implements LocaleAware interface. 检查给定的存储库是否实现LocaleAware接口。
      3. Inject request's _locale attribute into repository. 将请求的_locale属性注入存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM