Merge pull request #8 in WFCC/cc34 from 8-relation to master

* commit 'e5e24f823c67fec8fc7aee3fa7bd2508137dc1f8':
  Ajout de la relation Atelier User et fixture pour question 8
This commit is contained in:
Benjaballah Zakarya 2023-02-08 17:40:18 +01:00
commit 3b0d9a2d7c
7 changed files with 116 additions and 2 deletions

View File

@ -65,4 +65,11 @@ symfony console m:mig
symfony console d:m:m
symfony console make:auth
symfony console make:registration-form
```
### Question 8
```bash
symfony console make:entity
symfony console make:migration
```

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230208161544 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE atelier');
$this->addSql('CREATE TABLE atelier (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, instructeur_id INTEGER NOT NULL, nom VARCHAR(255) NOT NULL, description VARCHAR(1024) DEFAULT NULL, CONSTRAINT FK_E1BB182325FCA809 FOREIGN KEY (instructeur_id) REFERENCES user (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_E1BB182325FCA809 ON atelier (instructeur_id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TEMPORARY TABLE __temp__atelier AS SELECT id, nom, description FROM atelier');
$this->addSql('DROP TABLE atelier');
$this->addSql('CREATE TABLE atelier (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, nom VARCHAR(255) NOT NULL, description VARCHAR(1024) DEFAULT NULL)');
$this->addSql('INSERT INTO atelier (id, nom, description) SELECT id, nom, description FROM __temp__atelier');
$this->addSql('DROP TABLE __temp__atelier');
}
}

View File

@ -29,6 +29,7 @@ class AtelierController extends AbstractController
public function new(Request $request, AtelierRepository $atelierRepository): Response
{
$atelier = new Atelier();
$atelier->setInstructeur($this->getUser());
$form = $this->createForm(AtelierType::class, $atelier);
$form->handleRequest($request);

View File

@ -3,6 +3,7 @@
namespace App\DataFixtures;
use App\Entity\Atelier;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
@ -11,9 +12,17 @@ class AtelierFixture extends Fixture
public function load(ObjectManager $manager): void
{
$faker = \Faker\Factory::create("fr_FR");
for($i=0; $i<= 20; $i++){
$user = new User();
$user->setEmail('test@hotmail.com')
->setNom("test")
->setPrenom("test")
->setPassword("");
$manager->persist($user);
for ($i = 0; $i <= 20; $i++) {
$atelier = new Atelier();
$atelier->setNom($faker->word)->setDescription("# " . $faker->sentence(3) . "\n" . $faker->paragraph());
$atelier->setNom($faker->word)
->setDescription("# " . $faker->sentence(3) . "\n" . $faker->paragraph())
->setInstructeur($user);
$manager->persist($atelier);
}
$manager->flush();

View File

@ -19,6 +19,10 @@ class Atelier
#[ORM\Column(length: 1024, nullable: true)]
private ?string $description = null;
#[ORM\ManyToOne(inversedBy: 'ateliersFormÃÃÃes')]
#[ORM\JoinColumn(nullable: false)]
private ?User $instructeur = null;
public function getId(): ?int
{
return $this->id;
@ -47,4 +51,16 @@ class Atelier
return $this;
}
public function getInstructeur(): ?User
{
return $this->instructeur;
}
public function setInstructeur(?User $instructeur): self
{
$this->instructeur = $instructeur;
return $this;
}
}

View File

@ -3,6 +3,8 @@
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
@ -35,6 +37,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(length: 255)]
private ?string $prenom = null;
#[ORM\OneToMany(mappedBy: 'instructeur', targetEntity: Atelier::class, orphanRemoval: true)]
private Collection $ateliersFormÃÃÃes;
public function __construct()
{
$this->ateliersFormÃÃÃes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
@ -129,4 +139,34 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this;
}
/**
* @return Collection<int, Atelier>
*/
public function getAteliersFormÃÃÃes(): Collection
{
return $this->ateliersFormÃÃÃes;
}
public function addAteliersFormE(Atelier $ateliersFormE): self
{
if (!$this->ateliersFormÃÃÃes->contains($ateliersFormE)) {
$this->ateliersFormÃÃÃes->add($ateliersFormE);
$ateliersFormE->setInstructeur($this);
}
return $this;
}
public function removeAteliersFormE(Atelier $ateliersFormE): self
{
if ($this->ateliersFormÃÃÃes->removeElement($ateliersFormE)) {
// set the owning side to null (unless already changed)
if ($ateliersFormE->getInstructeur() === $this) {
$ateliersFormE->setInstructeur(null);
}
}
return $this;
}
}

View File

@ -19,6 +19,10 @@
<th>Description</th>
<td>{{ atelier.description | raw }}</td>
</tr>
<tr>
<th>Email instructeur</th>
<td>{{ atelier.instructeur.email}}</td>
</tr>
</tbody>
</table>