mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
first impl of person Mover + add fixtures
This commit is contained in:
101
src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadHousehold.php
Normal file
101
src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadHousehold.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\DataFixtures\ORM;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Household\MoveMembersFactory;
|
||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||
|
||||
class LoadHousehold extends Fixture implements DependentFixtureInterface
|
||||
{
|
||||
private MoveMembersFactory $movementFactory;
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
private CONST NUMBER_OF_HOUSEHOLD = 10;
|
||||
|
||||
public function __construct(MoveMembersFactory $movementFactory, EntityManagerInterface $em)
|
||||
{
|
||||
$this->movementFactory = $movementFactory;
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function load(ObjectManager $manager)
|
||||
{
|
||||
$this->preparePersonIds();
|
||||
|
||||
for ($i=0; $i < self::NUMBER_OF_HOUSEHOLD; $i++) {
|
||||
$household = new Household();
|
||||
$manager->persist($household);
|
||||
|
||||
$movement = $this->movementFactory->createMovement($household);
|
||||
|
||||
// load adults
|
||||
$k = 0;
|
||||
foreach ($this->getRandomPersons(1, 3) as $person) {
|
||||
$date = \DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
|
||||
->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 = \DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
|
||||
->add(new \DateInterval('P'.\random_int(1, 200).'W'));
|
||||
$position = $this->getReference(LoadHouseholdPosition::CHILD);
|
||||
|
||||
$movement->addMovement($date, $person, $position, $k === 0, "self generated");
|
||||
$k++;
|
||||
}
|
||||
|
||||
foreach ($movement->getPersistable() as $obj) {
|
||||
print($obj->getStartDate()->format('Y-m-d'));
|
||||
$manager->persist($obj);
|
||||
}
|
||||
}
|
||||
|
||||
$manager->flush();
|
||||
}
|
||||
|
||||
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
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user