我正在使用批处理来提高我的性能。这意味着我在生成50之后插入我的对象,就像你在代码中看到的那样。但是,我需要恢复这些对象的所有Ids,因为我需要保存一些照片。(我使用ID来为照片命名)
...
$em->persist($contact);
if(($k % $batchsize)===0){
$em->flush();
$user_id=$user->getId();
$em->clear();
$user=$userManager->findById($user_id);
}
$k++;
}有人知道如何在完成此过程后恢复ids吗?
发布于 2015-11-13 17:00:45
好的,在你的评论之后:
我建议您创建一个postFlush侦听器。这样,您就可以检索插入的所有实体,并使用这些实体:
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̀中
services:
my.listener:
class: AppBundle\EventListener\ContactInserts
tags:
- { name: doctrine.event_listener, event: postFlush }但要注意,因为在下面的代码中存在一些逻辑问题:
...
$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++;https://stackoverflow.com/questions/33677741
复制相似问题