使用Gedmo翻译OnFlush事件

时间:2015-07-17 14:01:12

标签: symfony

我的听众有问题。

public function onFlush(OnFlushEventArgs $args)
{
    ini_set('max_execution_time', 30000);
    ini_set('memory_limit', '512M');

    $em = $args->getEntityManager();
    $uow = $em->getUnitOfWork();

    foreach ($uow->getScheduledEntityInsertions() as $entity) {
        if($entity->getTranslatable() == 1){
            $translation = $this->translate($entity);

            $repository = $em->getRepository('ByJM\AdminBundle\Entity\Translation\ChambreTranslation');

            $repository
                ->translate($entity, 'nom', 'fr', $translation['fr'])
                ->translate($entity, 'nom', 'en', $translation['en'])
                ->translate($entity, 'nom', 'es', $translation['es'])
                ->translate($entity, 'nom', 'it', $translation['it'])
                ->translate($entity, 'nom', 'nl', $translation['nl'])
                ->translate($entity, 'nom', 'pt', $translation['pt'])
            ; 

            $md = $em->getClassMetadata('ByJM\AdminBundle\Entity\Chambre');
            $uow->recomputeSingleEntityChangeSet($md, $entity);
        }
    }

    foreach ($uow->getScheduledEntityUpdates() as $entity) {
        if($entity->getTranslatable() == 1){
            $translation = $this->translate($entity);

            $repository = $em->getRepository('ByJM\AdminBundle\Entity\Translation\ChambreTranslation');

            $repository
                ->translate($entity, 'nom', 'fr', $translation['fr'])
                ->translate($entity, 'nom', 'en', $translation['en'])
                ->translate($entity, 'nom', 'es', $translation['es'])
                ->translate($entity, 'nom', 'it', $translation['it'])
                ->translate($entity, 'nom', 'nl', $translation['nl'])
                ->translate($entity, 'nom', 'pt', $translation['pt'])
            ; 

            $em->persist($entity);

            $md = $em->getClassMetadata('ByJM\AdminBundle\Entity\Chambre');
            $uow->recomputeSingleEntityChangeSet($md, $entity);
        }
    }         
}

我有这个错误: 执行'INSERT INTO chambre_translations(locale,object_class,field,foreign_key,content)VALUES(?,?,?,?,?)'时发生异常:

SQLSTATE [HY093]:参数号无效:没有绑定参数

如果有人为我提供解决方案,那就太棒了!!!

提前致谢。

1 个答案:

答案 0 :(得分:0)

这不是与Gedmo相关的问题。

this post中所述,onFlush被调用,所有更改都已计算,如果您更改实体或创建新实体,则需要刷新它们。

因此您需要添加$em->persist($entity);

你能试试吗?请

public function onFlush(OnFlushEventArgs $args)
{
     $em = $args->getEntityManager();
     $uow = $em->getUnitOfWork();

     $entities = array_merge(
         $uow->getScheduledEntityInsertions(),
         $uow->getScheduledEntityUpdates()
     );

     foreach ($entities as $entity) {
         if($entity->getTranslatable() == 1){
               $translation = $this->translate($entity);

               $repository = $em->getRepository('ByJM\AdminBundle\Entity\Translation\ChambreTranslation');

               $repository
                     ->translate($entity, 'nom', 'fr', $translation['fr'])
                     ->translate($entity, 'nom', 'en', $translation['en'])
                     ->translate($entity, 'nom', 'es', $translation['es'])
                     ->translate($entity, 'nom', 'it', $translation['it'])
                     ->translate($entity, 'nom', 'nl', $translation['nl'])
                     ->translate($entity, 'nom', 'pt', $translation['pt']); 

               $em->persist($entity);
               $md = $em->getClassMetadata(get_class($entity));
               $uow->recomputeSingleEntityChangeSet($md, $entity);
         }

     }
}