protected function addUsersToCourses(InputInterface $input, OutputInterface $output)
 {
     $em = $this->doctrine->getManager();
     $maxuserspercourse = $input->getArgument('maxuserspercourse');
     $courselist = $this->doctrine->getRepository('AppBundle:Course')->findAll();
     foreach ($courselist as $course) {
         if ($count = count($course->getUsers())) {
             $output->writeln(sprintf('<info>Skipping course %s because it already has %d enrolments</info>', $course->getName(), $count));
         }
         $target = rand(1, $maxuserspercourse);
         $count = 0;
         $userlist = $this->doctrine->getRepository('AppBundle:Person')->findAll();
         shuffle($userlist);
         while ($count < $target && count($userlist)) {
             $user = array_shift($userlist);
             if ($course->hasUser($user)) {
                 continue;
             }
             $course->addUser($user);
         }
         $em->persist($course);
         $em->flush();
     }
 }
Ejemplo n.º 2
0
 /**
  *
  * @param string $entityClass
  * @return \Doctrine\ORM\EntityRepository The repository class.
  */
 public function getRepository($entityClass)
 {
     return Doctrine::getRepository($entityClass);
 }