diff --git a/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadHousehold.php b/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadHousehold.php index 8dfca71f6..029a71cfa 100644 --- a/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadHousehold.php +++ b/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadHousehold.php @@ -9,6 +9,7 @@ use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\ORM\EntityManagerInterface; use Doctrine\Persistence\ObjectManager; use Doctrine\Common\DataFixtures\DependentFixtureInterface; +use Nelmio\Alice\Loader\NativeLoader; class LoadHousehold extends Fixture implements DependentFixtureInterface { @@ -16,12 +17,15 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface private EntityManagerInterface $em; + private NativeLoader $loader; + private CONST NUMBER_OF_HOUSEHOLD = 10; public function __construct(MembersEditorFactory $editorFactory, EntityManagerInterface $em) { $this->editorFactory = $editorFactory; $this->em = $em; + $this->loader = new NativeLoader(); } public function load(ObjectManager $manager) @@ -53,6 +57,8 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface $household = new Household(); $manager->persist($household); + $this->addAddressToHousehold($household, \DateTimeImmutable::createFrom($startDate), $manager); + $movement = $this->editorFactory->createEditor($household); // load adults @@ -89,6 +95,55 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface } } + private function addAddressToHousehold(Household $household, \DateTimeImmutable $date, ObjectManager $manager) + { + if (\random_int(0, 10) > 8) { + // 20% of household without address + return; + } + + $nb = \random_int(1, 6); + + $i = 0; + while ($i < $nb) { + $address = $this->createAddress(); + $address->validFrom($date); + + if (\random_int(0, 20) < 1) { + $date = $date->add(new \DateInterval('P'.\random_int(8, 52).'W')); + $address->validTo($date); + } + + $household->addAddress($address); + $manager->persist($address); + + $date = $date->add(new \DateInterval('P'.\random_int(8, 52).'W')); + $i++; + } + } + + private function createAddress() + { + $objectSet = $this->loader->loadData([ + Address::class => [ + 'address1' => [ + 'street' => '', + 'streetNumber' => '', + 'postCode' => $this->getPostalCode() + ] + ] + ]); + + return $objectSet->getObjects()[0]; + } + + private function getPostalCode(): PostalCode + { + $ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)]; + + return $this->getReference($ref); + } + private function preparePersonIds() { $this->personIds = $this->em diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php index 0ef2bbb3c..02ea6e66b 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php @@ -68,13 +68,14 @@ class Household */ public function addAddress(Address $address) { - $this->addresses[] = $address; - foreach ($this->getAddresses() as $a) { if ($a->getValidFrom() < $address->getValidFrom() && $a->getValidTo() === NULL) { $a->setValidTo($address->getValidFrom()); } } + + $this->addresses[] = $address; + return $this; } diff --git a/src/Bundle/ChillThirdPartyBundle/DataFixtures/ORM/LoadThirdParty.php b/src/Bundle/ChillThirdPartyBundle/DataFixtures/ORM/LoadThirdParty.php index 63f6e9c3b..7fc5ea1c8 100644 --- a/src/Bundle/ChillThirdPartyBundle/DataFixtures/ORM/LoadThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/DataFixtures/ORM/LoadThirdParty.php @@ -88,17 +88,8 @@ class LoadThirdParty extends Fixture Implements DependentFixtureInterface private function getPostalCode(): PostalCode { $ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)]; + return $this->getReference($ref); - if (count($this->postalCodesIds) === 0) { - // fill the postal codes - $this->em->createQuery('SELECT p.id FROM '.PostalCode::class) - ->getScalarResult(); - } - - $id = $this->postalCodesIds[\array_rand($this->postalCodesIds)]; - - return $this->em->getRepository(PostalCode::class) - ->find($id); } private function createAddress(): ObjectSet