Add species fixture
This commit is contained in:
34
src/DataFixtures/AppFixtures.php
Normal file
34
src/DataFixtures/AppFixtures.php
Normal 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();
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user