多次冲洗与学说

时间:2014-12-16 16:45:54

标签: php symfony doctrine-orm

我必须从.csv文件加载用户/学生列表。 每个学生都是一个促销活动的成员。

在文件中我有用户的信息和他的促销名称。 所以我必须在插入学生之前插入所有促销活动。这部分有效。

protected function execute(InputInterface $input, OutputInterface $output)
{
    /* WORKS FROM THIS */
    $start = new \DateTime();
    $output->writeln('[START] time : ' . $start->format('H:i:s'));
    $f = fopen($input->getArgument('filename'), 'r');

    if ($input->getOption('has-headers'))
        fgetcsv($f, null, ',');

    $doctrine = $this->getContainer()->get('doctrine');
    $em = $doctrine->getManager();
    $um = $this->getContainer()->get('fos_user.user_manager');

    $promoRepo = $em->getRepository('PlanningBundle:Promotion');
    $promotions = $promoRepo->findAll();
    /** @var Promotion[] $promosIDX */
    $promosIDX = array();

    foreach ($promotions as $promotion) {
        $promosIDX[$promotion->getName()] = $promotion;
    }

    while ($user = fgetcsv($f)) {

        if (!array_key_exists($user[7], $promosIDX)) {
            $promotion = new Promotion();
            $promotion->setName($user[7])
                ->setDateEnd(new \DateTime())
                ->setDateStart(new \DateTime());

            $em->persist($promotion);
        }
    }

    $em->flush();
    $em->clear();

    $doctrine->resetManager();
    $em = $doctrine->getManager();
    /* TO THIS */
    $f = fopen($input->getArgument('filename'), 'r');

    if ($input->getOption('has-headers'))
        fgetcsv($f, null, ',');

    $promoRepo = $em->getRepository('PlanningBundle:Promotion');

    $userRepo = $em->getRepository('PlanningBundle:User');

    $i = 0;
    while ($user = fgetcsv($f)) {

        $student = new Student();
        $student
            ->setUsername($user[0])
            ->setEmail($user[1])
            ->setLastname($user[2])
            ->setFirstname($user[3])
            ->setPromotion( $promoRepo->findOneByName($user[7]) )
            ->setPassword('insta' . (new \DateTime())->format('Y'))
        ;

        if( is_null($userRepo->findOneByUsername($student->getUsername())) ) {
            $i++;
            $um->updateUser($student, false);
        }

        if (($i % 20) == 0) {
            try {
                $em->flush();
                $em->clear();
            } catch (\Exception $e) {
                $output->writeln('[ERROR] reason : ' . $e->getMessage());
            }
        }

    }

    try {
        $em->flush();
        $em->clear();
    } catch (\Exception $e) {
        $output->writeln('[ERROR] reason : ' . $e->getMessage());
    }

    $end = new \DateTime();
    $diff = $end->diff($start);
    $output->writeln( "[END] duration : " . $diff->h . ":" .$diff->i .":" .$diff->s );

}

这是对的吗?我尝试了很多种可能性,并且有多个例外...... 而且这个没有插入任何用户。

2 个答案:

答案 0 :(得分:1)

我用过:

$em->flush();
$em->clear();
$em = $this->getContainer()->get('doctrine')->getManager();

为了正确重新加载经理。它解决了问题,并允许避免实体管理器已关闭异常。

答案 1 :(得分:0)

在FOSUserBundle文档中,您必须使用$um->createUser()而不是new Student()创建新用户:

$student = $um->createUser();

This is also noted in the updateUser() method.