editorFactory = $editorFactory; $this->em = $em; } public function load(ObjectManager $manager) { // generate two times the participation. This will lead to // some movement in participation (same people in two differents // households) $this->preparePersonIds(); $this->generateHousehold( $manager, \DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01') ); $this->preparePersonIds(); $this->generateHousehold( $manager, \DateTimeImmutable::createFromFormat('Y-m-d', '2015-01-01') ); $manager->flush(); } private function generateHousehold(ObjectManager $manager, \DateTimeImmutable $startDate) { for ($i=0; $i < self::NUMBER_OF_HOUSEHOLD; $i++) { $household = new Household(); $manager->persist($household); $movement = $this->editorFactory->createEditor($household); // load adults $k = 0; foreach ($this->getRandomPersons(1, 3) as $person) { $date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W')); $position = $this->getReference(LoadHouseholdPosition::ADULT); $movement->addMovement($date, $person, $position, $k === 0, "self generated"); $k++; } // load children foreach ($this->getRandomPersons(0, 3) as $person) { $date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W')); $position = $this->getReference(LoadHouseholdPosition::CHILD); $movement->addMovement($date, $person, $position, $k === 0, "self generated"); $k++; } // load children out foreach ($this->getRandomPersons(0, 2) as $person) { $date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W')); $position = $this->getReference(LoadHouseholdPosition::CHILD_OUT); $movement->addMovement($date, $person, $position, $k === 0, "self generated"); $k++; } foreach ($movement->getPersistable() as $obj) { $manager->persist($obj); } } } private function preparePersonIds() { $this->personIds = $this->em ->createQuery('SELECT p.id FROM '.Person::class.' p '. 'JOIN p.center c '. 'WHERE c.name = :center ' ) ->setParameter('center', 'Center A') ->getScalarResult() ; \shuffle($this->personIds); } private function getRandomPersons(int $min, int $max) { $nb = \random_int($min, $max); for ($i=0; $i < $nb; $i++) { $personId = \array_pop($this->personIds)['id']; $persons[] = $this->em->getRepository(Person::class) ->find($personId) ; } return $persons ?? []; } public function getDependencies() { return [ LoadPeople::class, LoadHouseholdPosition::class ]; } }