gpt4 book ai didi

symfony - Gedmo Doctrine Extensions - Sluggable + Translatable Yaml 配置

转载 作者:行者123 更新时间:2023-12-03 20:55:01 29 4
gpt4 key购买 nike

我正在尝试使用 gedmo 学说扩展来翻译实体。
https://github.com/Atlantic18/DoctrineExtensions
我使用 yml 作为 orm 映射文件(自动生成实体)。
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
我可以毫无问题地翻译标题。但是 slug 不起作用...
通常,在默认语言 (tr) 上,slug 会自动生成,我没有任何生成过程。
实体文件:
<?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;
}
}
文档中有一部分:
https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sluggable.md#using-translationlistener-to-translate-our-slug
我不知道如何应用这些听众。
我应该怎么做才能自动翻译 slug?

编辑
我的 config.yml 用于学说和 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

最佳答案

经过一番挣扎,我想我已经设法找到了解决您问题的方法。让我们谈正事吧。

首先,我注意到你在使用 StofDoctrineExtensionsBundle 这是 的包装器Gedmo Doctrine2 扩展 .

安装/使用似乎更容易,但从长远来看,它会使问题复杂化。例如,在这种情况下,为了解决您的问题,您必须修改监听器,并且使用该捆绑包,我能想到的唯一解决方案是破解代码并手动更改服务的优先级。这导致第一个解决方案:

第一个解决方案:破解包(不是一个好的)

这些服务可以在

StofDoctrineExtensionsBundle/Resources/config/listeners.xml



您需要找到并修改可插入服务并提高其优先级。这样,此服务将在可翻译服务之前执行。将首先创建 slug,translatable 会很好地存储它。

修改(没试过,我觉得可能有用):
<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>

但是,我不喜欢这种解决方案。老实说,我不喜欢 wrapper 。我将给你另一个我觉得更令人满意的解决方案(我尝试过并且有效)。

第二个解决方案(也是最好的一个)

首先,摆脱 StofDoctrineExtensionsBundle。其次,安装 Gedmo Doctrine2 扩展(如何操作 here)。

您会注意到,为了安装扩展,您必须在包中创建一个包含必要服务的文件。那很好。转到该文件并修改 sluggable 服务的优先级:
gedmo.listener.sluggable:
class: Gedmo\Sluggable\SluggableListener
tags:
- { name: doctrine.event_subscriber, connection: default, priority: 1 }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]

现在它准备好了。

这些是实体 Post 的 yml 和 php(我认为它们与您的非常相似,但可能有所不同):

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

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;
}
}

当您创建和实体并将其存储在数据库中时,它将创建 slug 并将标题和 slug 存储在翻译表中(如果您更改语言环境)。

如果你试试:
$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();

你会看到你的翻译表有两行,一行是标题,另一行是本地 de_de 的 slug。

希望它有所帮助和亲切的问候。

关于symfony - Gedmo Doctrine Extensions - Sluggable + Translatable Yaml 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25441935/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com