问题描述
我正在尝试使用gedmo原则扩展翻译实体。
我使用yml作为orm映射文件(自动生成实体)。
orm.yml:
CS\ContentBundle\Entity\Post:
类型:entity
表:posts
repositoryClass:CS\ContentBundle\Entity\PostRepository
gedmo:
soft_deleteable:
field_name:deleted_at
翻译:
locale:locale
fields:
id:
类型:整数
长度:11
id:true
生成器:
策略:AUTO
标题:
类型:字符串
长度:500
gedmo:
- 可翻译
slug:
类型:string
长度:500
gedmo:
可翻译:{}
s接收器:
分隔符: -
字段:
- 标题
我可以翻译标题没有问题。但是slug不工作...
通常情况下,在默认语言(tr)中,slug自动生成,没有任何生成过程。
实体文件:
<?php
命名空间CS\ ContentBundle\Entity;
使用Doctrine\ORM\Mapping作为ORM;
使用Gedmo\Translatable\Translatable;
使用Gedmo\Mapping\Annotation作为Gedmo;
使用APY\DataGridBundle\Grid\Mapping作为GRID;
/ **
*发布
* @ Gedmo\SoftDeleteable(fieldName =deleted_at,timeAware = false)
* @ GRID\Source(columns =id,title,is_active,created_at,updated_at)
* /
class Post implements Translatable
{
/ **
* @var integer
* /
private $ id;
/ **
* @var string
* @ Gedmo\Translatable
* @ GRID\Column(title =Başlık,filterable = true)
* /
private $ title;
/ **
* @var string
* @ Gedmo\Translatable
* /
private $ slug;
/ **
* @ Gedmo\Locale
* /
private $ locale;
public function setLocale($ locale)
{
$ this-> locale = $ locale;
}
/ **
*获取ID
*
* @return integer
* /
public function getId()
{
return $ this-> id;
}
/ **
*设置标题
*
* @param string $ title
* @return发布
* /
public function setTitle($ title)
{
$ this-> title = $ title;
return $ this;
}
/ **
*获取标题
*
* @return string
* /
public function getTitle )
{
return $ this-> title;
}
/ **
*设置slug
*
* @param string $ slug
* @return发布
* /
public function setSlug($ slug)
{
$ this-> slug = $ slug;
return $ this;
}
/ **
*获取slug
*
* @return string
* /
public function getSlug )
{
return $ this-> slug;
}
}
文档中有一部分:
我不知道如何申请这些听众。
我该怎么做自动翻译s子?
EDIT
我的config.yml for doctrine和stof_doctrine_extensions:
doctrine:
dbal:
default_connection:默认
连接:
默认值:
驱动程序:%database_driver%
主机:%database_host $
port:%database_port%
dbname:%database_name%
user:%database_user%
密码:%database_password%
charset:UTF8
mapping_types:
枚举:string
orm:
auto_generate_proxy_classes:%kernel.debug%
auto_mapping:true
metadata_cache_driver:redis
query_cache_driver:redis
过滤器:
softdeleteable:
类:Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
启用:true
映射:
StofDoctrineExtensionsBundle:〜
gedmo_translatable:
类型:注释
前缀:Gedmo\Translatable\Entity
dir :%kernel.root_dir%/ .. / vendor / gedmo / doctrine-extensions / lib / Gedmo /可翻译/实体
别名:GedmoTranslatable#这个是可选的,默认为映射设置的名称
is_bundl e:false
gedmo_tree:
类型:注释
前缀:Gedmo\Tree\Entity
dir:%kernel.root_dir%/ .. / vendor / gedmo / doctrine -extensions / lib / Gedmo / Tree / Entity
别名:GedmoTree#这一个是可选的,默认为映射设置的名称
is_bundle:false
stof_doctrine_extensions:
default_locale:%locale%
translation_fallback:true
orm:
默认值:
树:true
sluggable:true
可翻译: true
timestampable:true
softdeleteable:true
经过一番努力,我想我已经找到了解决你的问题的办法。让我们来做生意。
首先,我注意到你使用的是 StofDoctrineExtensionsBundle ,这是 Gedmo Doctrine2扩展程序。
似乎更容易安装/使用,但从长远来看,它使事情变得复杂。在这种情况下,例如,为了解决您的问题,您必须修改监听器,并且使用该捆绑包,我可以想出的唯一解决方案是破解代码并手动更改服务的优先级。这导致了第一个解决方案:
第一个解决方案:打包(不是很好)
可以在
您需要查找和修改sluggable服务并提高其优先级。这样,这个服务将在可翻译服务之前执行。 sㄧlug created created created and。。。。。。。。。。。。。。。>>>>>)))))))))))))))))))))))))))))) b
< service id =stof_doctrine_extensions.listener.sluggableclass =%stof_doctrine_extensions.listener.sluggable.class%public =false>
< tag name =kernel.cache_warmerpriority =1/>
< call method =setAnnotationReader>
< argument type =serviceid =annotation_reader/>
< / call>
< / service>
但是,我不喜欢这个解决方案。说实话,我不喜欢包装纸。我会给你一个解决方案,我觉得更令人满意(我试过它并且有效)。
第二个解决方案(和最好的) / strong>
首先,摆脱StofDoctrineExtensionsBundle。其次,安装Gedmo Doctrine2扩展(如何做到这一点)
您会注意到,为了安装扩展程序,您必须在包含必需服务的包中创建一个文件。那很好。转到该文件并修改sluggable服务的优先级:
gedmo.listener.sluggable:
class:Gedmo \Sluggable\SluggableListener
标签:
- {name:doctrine.event_subscriber,connection:default,priority:1}
调用:
- [setAnnotationReader,[@annotation_reader] ]
现在就可以了。
这些是我的yml和php的实体Post(我认为他们真的类似于你的,但可以不同):
YML p>
DSG\AcmeBundle\Entity\Post:
类型:实体
表:帖子
gedmo:
soft_deleteable:
field_name:deleted_at
翻译:
语言环境:locale
字段:
id:
类型:整数
长度:11
id:true
生成器:
策略:AUTO
标题:
类型:string
长度:255
gedmo:
- 可翻译
slug:
类型:string
length:255
unique:true
gedmo:
可翻译:{}
slug:
分隔符:_
style:camel
字段:
- 标题
而 PHP
命名空间DSG\AcmeBundle\Entity;
使用Gedmo\Translatable\Translatable;
class Post implements Translatable
{
/ **
* @var integer
* /
private $ id;
/ **
* @var string
* /
private $ title;
/ **
* @var string
* /
private $ slug;
/ **
* @var string
* /
private $ locale;
public function setTranslatableLocale($ locale)
{
$ this-> locale = $ locale;
}
/ **
*获取ID
*
* @return integer
* /
public function getId()
{
return $ this-> id;
}
/ **
*设置标题
*
* @param string $ title
* @return发布
* /
public function setTitle($ title)
{
$ this-> title = $ title;
return $ this;
}
/ **
*获取标题
*
* @return string
* /
public function getTitle )
{
return $ this-> title;
}
/ **
*设置slug
*
* @param string $ slug
* @return发布
* /
public function setSlug($ slug)
{
$ this-> slug = $ slug;
return $ this;
}
/ **
*获取slug
*
* @return string
* /
public function getSlug )
{
return $ this-> slug;
}
}
当您创建并实体并将其存储在数据库中时,它将创建s lug the the the in in in in。。。。。。。。。。。>>>>>>>>>>>>>>>>>>>>>>>>>
$ b
$ em = $ this-> getDoctrine() - > getEntityManager();
$ article = new Post();
$ article-> setTitle('the title');
$ em-> persist($ article);
$ em-> flush();
$ article = $ this-> getDoctrine() - > getRepository('DSGMDayBundle:Post') - > find(1);
$ article-> setTitle('我的标题在de');
$ article-> setTranslatableLocale('de_de'); //更改语言环境
$ em-> persist($ article);
$ em-> flush();
你会看到你的翻译表有两行,一个是标题,另一个是
希望它有帮助和善意的问候。
I'm trying to translate entities with gedmo doctrine extensions.
https://github.com/Atlantic18/DoctrineExtensions
I'm using yml as orm mapping file (auto generating entities).
orm.yml:
CS\ContentBundle\Entity\Post:
type: entity
table: posts
repositoryClass: CS\ContentBundle\Entity\PostRepository
gedmo:
soft_deleteable:
field_name: deleted_at
translation:
locale: locale
fields:
id:
type: integer
length: 11
id: true
generator:
strategy: AUTO
title:
type: string
length: 500
gedmo:
- translatable
slug:
type: string
length: 500
gedmo:
translatable: {}
slug:
separator: -
fields:
- title
I can translate the title without a problem. But slug is not working...
Normally, on default language (tr), slug auto generated without any generation process by me.
Entity file:
<?php
namespace CS\ContentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;
use Gedmo\Mapping\Annotation as Gedmo;
use APY\DataGridBundle\Grid\Mapping as GRID;
/**
* Post
* @Gedmo\SoftDeleteable(fieldName="deleted_at", timeAware=false)
* @GRID\Source(columns="id,title,is_active,created_at,updated_at")
*/
class Post implements Translatable
{
/**
* @var integer
*/
private $id;
/**
* @var string
* @Gedmo\Translatable
* @GRID\Column(title="Başlık", filterable=true)
*/
private $title;
/**
* @var string
* @Gedmo\Translatable
*/
private $slug;
/**
* @Gedmo\Locale
*/
private $locale;
public function setLocale($locale)
{
$this->locale = $locale;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set slug
*
* @param string $slug
* @return Post
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
}
There is a part in documentation:https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sluggable.md#using-translationlistener-to-translate-our-slug
I don't know how to apply these listeners.
What should i do to translate the slug automatically?
EDIT
My config.yml for doctrine and stof_doctrine_extensions:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types:
enum: string
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
metadata_cache_driver: redis
query_cache_driver: redis
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
mappings:
StofDoctrineExtensionsBundle: ~
gedmo_translatable:
type: annotation
prefix: Gedmo\Translatable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
alias: GedmoTranslatable # this one is optional and will default to the name set for the mapping
is_bundle: false
gedmo_tree:
type: annotation
prefix: Gedmo\Tree\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
alias: GedmoTree # this one is optional and will default to the name set for the mapping
is_bundle: false
stof_doctrine_extensions:
default_locale: "%locale%"
translation_fallback: true
orm:
default:
tree: true
sluggable: true
translatable: true
timestampable: true
softdeleteable: true
After a bit of struggling, I think I've managed to find a solution to your problem. Let's get down to business.
First of all, I've noticed you're using StofDoctrineExtensionsBundle which is a wrapper of the Gedmo Doctrine2 extensions.
It might have seemed easier to install/use but in the long run it complicates matters. In this case, for example, in order to solve your problem you have to modify listeners and with that bundle the only solution I can come up with is to hack the code and change the priority of the service manually. This leads to the first solution:
First solution: hack the bundle (not a good one)
The services can be found in
You need to find and modify the sluggable service and increase its priority. That way, this service will execute before the translatable service. The slug would be created first and translatable would store it nicely.
Modification (haven't tried it though I think it might work):
<service id="stof_doctrine_extensions.listener.sluggable" class="%stof_doctrine_extensions.listener.sluggable.class%" public="false">
<tag name="kernel.cache_warmer" priority="1" />
<call method="setAnnotationReader">
<argument type="service" id="annotation_reader" />
</call>
</service>
However, I don't like this solution. To be honest, I don't like wrappers. I'm going to give you another solution that I find more satisfactory (and I tried it and works).
Second solution (and the best one)
First off, get rid of StofDoctrineExtensionsBundle. Secondly, install Gedmo Doctrine2 extensions (how to do it here).
You'll notice that in order to install the extensions you had to create a file in your bundle containing the necessary services. That is good. Go to that file and modify the priority for the sluggable service:
gedmo.listener.sluggable:
class: Gedmo\Sluggable\SluggableListener
tags:
- { name: doctrine.event_subscriber, connection: default, priority: 1 }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
And now it is ready.
These are my yml and php for the entity Post (I think they are really similar to yours but can differ):
YML
DSG\AcmeBundle\Entity\Post:
type: entity
table: posts
gedmo:
soft_deleteable:
field_name: deleted_at
translation:
locale: locale
fields:
id:
type: integer
length: 11
id: true
generator:
strategy: AUTO
title:
type: string
length: 255
gedmo:
- translatable
slug:
type: string
length: 255
unique: true
gedmo:
translatable: {}
slug:
separator: _
style: camel
fields:
- title
And the PHP
namespace DSG\AcmeBundle\Entity;
use Gedmo\Translatable\Translatable;
class Post implements Translatable
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $slug;
/**
* @var string
*/
private $locale;
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set slug
*
* @param string $slug
* @return Post
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
}
When you create and entity and store it in the database, it will create the slug and store the title and the slug in the translation table (if you change the locale).
If you try:
$em = $this->getDoctrine()->getEntityManager();
$article = new Post();
$article->setTitle('the title');
$em->persist($article);
$em->flush();
$article = $this->getDoctrine()->getRepository('DSGMDayBundle:Post')->find(1);
$article->setTitle('my title in de');
$article->setTranslatableLocale('de_de'); // change locale
$em->persist($article);
$em->flush();
You'll see that your translation table gets two rows, one for the title and the other for the slug for the local de_de.
Hope it helps and kind regards.
这篇关于Gedmo原则扩展 - 可压缩+可翻译的Yaml配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!