使用翻译行为时如何查询翻译内容? [英] How to query translated content when using the translate behavior?

查看:110
本文介绍了使用翻译行为时如何查询翻译内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有多种语言,因此文章标题取决于当地。但是有一个问题:我如何搜索另一种语言的文章?

My website is in multiple languages so an article's title depends on the local. But there's a problem: How can I search for an article in another language?

现在,唯一的方法是用英文输入标题,以便cakePHP检索名称以法语为例。我无法用法语搜索。

Right now, the only way is by typing the title in english so that cakePHP retrieves the name in french, for exmple. I can't search for it in french.

例如:
当我搜索 Hello时,我发现了一篇名为 Bonjour $ b $的文章。 b但是当我搜索 Bonjour时,找不到任何文章。

For exemple: When I search for "Hello" I find the article named "Bonjour" But when I search for "Bonjour" I can't find any article.

那我该如何用另一种语言搜索?似乎Cakephp首先使用默认语言进行搜索,然后得到这些推导。

So how do I search in another language? It seem that Cakephp search first in default language to then get the traductions.

在我的控制器中:

$this->Ingredients->locale('fr_CA');
$data = $this->Ingredients->find('all')
->select([
    'Ingredients.id',
    'Ingredients.name'
])
->where(["Ingredients.name LIKE '%".$this->request->query['k']."%'"])
->order('Ingredients.recipe_count');


推荐答案

SQL注入!


首先,当您将用户数据插入SQL片段而不是使用 key =>时,您就有一个SQL注入漏洞。值格式,这将导致带有绑定值的正确查询!

SQL Injections!

First things first, what you have there is a SQL injection vulnerability, as you are inserting user data into an SQL fragment instead of using the key => value format, which would result in proper queries with bound values!

请务必阅读 食谱>数据库访问与ORM>查询生成器>高级条件 ,以获取有关如何正确构建高级条件的更多信息!

Please be sure to read Cookbook > Database Access & ORM > Query Builder > Advanced Conditions for more information on how to properly build advanced conditions!

通过翻译内容搜索,那么,翻译行为会为每个可翻译字段添加 hasOne 关联,默认情况下,命名方案为 TableAlias_field_translation ,因此例如 Ingredients_name_translation

Searching by translated content, well, the Translate behavior adds a hasOne association for each translatable field, and by default the naming scheme is TableAlias_field_translation, so for example Ingredients_name_translation.

关联已自动包含,因此您可以搜索其内容字段,它可能包含翻译后的内容。

The association is being contained automatically, so you can search on its content field, which will possibly hold the translated content.

从CakePHP 3.4.0(分别为3.4.4)开始,您可以使用 translationField ()方法(由行为转换提供),它将根据区域设置返回正确的别名字段:

As of CakePHP 3.4.0, respectively 3.4.4, you can use the translationField() method provided by the translate behavior, it will return the properly aliased field depending on the locale:

->where([
    $this->Ingredients->translationField('name') . ' LIKE' =>
        '%' . $this->request->query('k') . '%'
])

请注意,在3.4.4之前, translationField()不会考虑语言环境集与默认语言环境匹配的情况!在这种情况下,您可能想查询原始表 name 列,但是在3.4.4之前,该方法将始终返回 content

Note that before 3.4.4, translationField() would not consider the case where the locale set matches the default locale! In that situation you would want to query against the original tables name column, but before 3.4.4 the method would always return the content column of the associated translation table.

另请参见 菜谱>数据库访问与ORM>行为>翻译>查询翻译的字段

See also Cookbook > Database Access & ORM > Behaviors > Translate > Querying Translated Fields

在CakePHP 3.4之前的版本中,您必须自己构建列名,例如:

In versions before CakePHP 3.4, you'll have to build the column name yourself, like:

->where([
    'Ingredients_name_translation.content LIKE' => '%' . $this->request->query['k'] . '%'
])

在上述情况下,如果定位集与默认语言匹配,则翻译行为将不包含翻译表关联,因此您必须根据所使用的语言(即使用 Ingredients)采取适当的措施以确保条件中使用了正确的字段。名称,以防使用默认语言!

In the case mentioned above, where the locate set matches the default language, the Translate behavior won't contain the translation table associations, so you'd have to take appropriate measures to ensure that the correct field is being used in the conditions, based on the language in use, ie use Ingredients.name in case the default language is being used!

这篇关于使用翻译行为时如何查询翻译内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆