简体   繁体   English

Sonata Admin - 如何将翻译添加到 object 的一个字段和 getID?

[英]Sonata Admin - how to add Translation to one field and getID of the object?

My code:我的代码:

public function create($object): void
{
    /** @var CarsEvent $object */
    $carsEvent = $object;
    $em          = $this->getEntityManager($carsEvent );

    $carsEvent->addTranslation(new CarsEventTranslation());
    
    $em->persist($sportsEvent);
    $em->flush();

}

This creates a new car object and save it into my database, and that works fine, however in this part of the code I'm calling and translation function which needs to add a translation for the NAME field into my translation table for the german language这将创建一辆新车 object 并将其保存到我的数据库中,并且工作正常,但是在这部分代码中我正在调用和翻译 function 需要将 NAME 字段的翻译添加到我的德语翻译表中

/**
 * {@inheritdoc}
 */
public function addTranslation(Translation $translation)
{

    $translation->setLocale('de');
    $translation->setField('name');
    $translation->setContent('FixedGermanName');

    $this->translations[] = $translation;

    return $this;
}

This also works but I cannot get the objectId, so my translation table in the database is not connected with the id of object that I created, I have a function这也有效,但我无法获取 objectId,因此我在数据库中的翻译表与我创建的 object 的 id 没有连接,我有一个 function

$carsEvent->getId();

but always return NULL但总是返回 NULL

My question is how to get the object id so I can store that value in my database too?我的问题是如何获取 object id,以便我也可以将该值存储在我的数据库中?

Solution:解决方案:

/**
 * {@inheritdoc}
 */
public function addTranslation(Translation $translation)
{

    $translation->setLocale('de');
    $translation->setField('name');
    $translation->setObject($translation);
    $translation->setContent('FixedGermanName');

    $this->translations[] = $translation;

    return $this;
}

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

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