mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
prevent duplicates in relationships data fixtures
This commit is contained in:
parent
372ec7c4b2
commit
eb2bc306f7
@ -21,6 +21,7 @@ use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
|||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\Persistence\ObjectManager;
|
use Doctrine\Persistence\ObjectManager;
|
||||||
|
|
||||||
|
use function array_key_exists;
|
||||||
use function count;
|
use function count;
|
||||||
|
|
||||||
class LoadRelationships extends Fixture implements DependentFixtureInterface
|
class LoadRelationships extends Fixture implements DependentFixtureInterface
|
||||||
@ -34,7 +35,7 @@ class LoadRelationships extends Fixture implements DependentFixtureInterface
|
|||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDependencies()
|
public function getDependencies(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
LoadPeople::class,
|
LoadPeople::class,
|
||||||
@ -42,9 +43,11 @@ class LoadRelationships extends Fixture implements DependentFixtureInterface
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function load(ObjectManager $manager)
|
public function load(ObjectManager $manager): void
|
||||||
{
|
{
|
||||||
for ($i = 0; 15 > $i; ++$i) {
|
$existing = [];
|
||||||
|
|
||||||
|
for ($i = 0; 20 > $i; ++$i) {
|
||||||
$user = $this->getRandomUser();
|
$user = $this->getRandomUser();
|
||||||
$date = new DateTimeImmutable();
|
$date = new DateTimeImmutable();
|
||||||
$relationship = (new Relationship())
|
$relationship = (new Relationship())
|
||||||
@ -57,6 +60,17 @@ class LoadRelationships extends Fixture implements DependentFixtureInterface
|
|||||||
->setUpdatedBy($user)
|
->setUpdatedBy($user)
|
||||||
->setCreatedAt($date)
|
->setCreatedAt($date)
|
||||||
->setUpdatedAt($date);
|
->setUpdatedAt($date);
|
||||||
|
|
||||||
|
// remove the potential duplicates
|
||||||
|
$set = $relationship->getFromPerson()->getId() < $relationship->getToPerson()->getId() ?
|
||||||
|
[$relationship->getFromPerson()->getId(), $relationship->getToPerson()->getId()] :
|
||||||
|
[$relationship->getToPerson()->getId(), $relationship->getFromPerson()->getId()];
|
||||||
|
|
||||||
|
if (array_key_exists($set[0], $existing) && array_key_exists($set[1], $existing[$set[0]])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$existing[$set[0]][$existing[$set[1]]] = 1;
|
||||||
$manager->persist($relationship);
|
$manager->persist($relationship);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user