首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用批处理的原理后获得所有的I?

如何在使用批处理的原理后获得所有的I?
EN

Stack Overflow用户
提问于 2015-11-13 01:36:07
回答 1查看 503关注 0票数 0

我正在使用批处理来提高我的性能。这意味着我在生成50之后插入我的对象,就像你在代码中看到的那样。但是,我需要恢复这些对象的所有Ids,因为我需要保存一些照片。(我使用ID来为照片命名)

代码语言:javascript
复制
    ...

    $em->persist($contact);

                    if(($k % $batchsize)===0){
                        $em->flush();
                        $user_id=$user->getId();
                        $em->clear();
                        $user=$userManager->findById($user_id);
                    }
                    $k++;
}

有人知道如何在完成此过程后恢复ids吗?

EN

回答 1

Stack Overflow用户

发布于 2015-11-13 17:00:45

好的,在你的评论之后:

我建议您创建一个postFlush侦听器。这样,您就可以检索插入的所有实体,并使用这些实体:

代码语言:javascript
复制
namespace AppBundle\EventListener

use Doctrine\ORM\Event\LifecycleEventArgs;
use AppBundle\Entity\Contact;

class ContactInserts {
    public function postFlush(LifecycleEventArgs $args) {
        $em = $args->getEntityManager();
        foreach ($em->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
            if ($entity instanceof Contact) {

                //... Do what you want to do with your contact ($entity) ...
                $em->persist($entity);
            }
        }
        $em->flush();
    }
}

在你的service.yml或`config.yml̀中

代码语言:javascript
复制
services:
    my.listener:
        class: AppBundle\EventListener\ContactInserts
        tags:
            - { name: doctrine.event_listener, event: postFlush }

但要注意,因为在下面的代码中存在一些逻辑问题:

代码语言:javascript
复制
...            
$em->persist($contact);
// /!\ If you don't have a 49 contacts left, these won't be flushed
if(($k % $batchsize)===0) {
     $em->flush();
     $em->clear();
     /* I can't understand what you're doing here sorry */
     $user_id=$user->getId(); //You use user to get the id
     $user=$userManager->findById($user_id); //You use the just retrieved id to get the user ???                   
}
$k++;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33677741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档