Add species fixture

This commit is contained in:
2024-05-22 17:16:38 +02:00
parent cfb53dad52
commit d4cc71946d
7 changed files with 342 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
<?php
namespace App\DataFixtures;
use App\Entity\Post;
use App\Entity\Species;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class AppFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$faker = \Faker\Factory::create();
for ($i = 0; $i < 20; ++$i) {
$name = $faker->text();
$species = (new Species())
->setScientificName($name)
->setVernacularName($name)
->setRegion($faker->country());
$date = DateTimeImmutable::createFromMutable($faker->dateTime());
$post = (new Post())
->setFoundDate($date)
->setPublicationDate($date)
->setLatitude($faker->randomFloat())
->setLongitude($faker->randomFloat())
->setCommentary($faker->text());
$manager->persist($species);
$manager->persist($post);
}
$manager->flush();
}
}

View File

@@ -31,6 +31,9 @@ class Post
#[ORM\Column(type: Types::TEXT)]
private ?string $commentary = null;
#[ORM\ManyToOne(inversedBy: 'posts')]
private ?Species $species = null;
public function getId(): ?int
{
return $this->id;
@@ -107,4 +110,16 @@ class Post
return $this;
}
public function getSpecies(): ?Species
{
return $this->species;
}
public function setSpecies(?Species $species): static
{
$this->species = $species;
return $this;
}
}

View File

@@ -3,6 +3,8 @@
namespace App\Entity;
use App\Repository\SpeciesRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SpeciesRepository::class)]
@@ -22,6 +24,17 @@ class Species
#[ORM\Column(length: 255)]
private ?string $region = null;
/**
* @var Collection<int, Post>
*/
#[ORM\OneToMany(targetEntity: Post::class, mappedBy: 'species')]
private Collection $posts;
public function __construct()
{
$this->posts = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
@@ -62,4 +75,34 @@ class Species
return $this;
}
/**
* @return Collection<int, Post>
*/
public function getPosts(): Collection
{
return $this->posts;
}
public function addPost(Post $post): static
{
if (!$this->posts->contains($post)) {
$this->posts->add($post);
$post->setSpecies($this);
}
return $this;
}
public function removePost(Post $post): static
{
if ($this->posts->removeElement($post)) {
// set the owning side to null (unless already changed)
if ($post->getSpecies() === $this) {
$post->setSpecies(null);
}
}
return $this;
}
}