1 pièce(s) jointe(s)
ManyToOne pour récupérer une donnée
Bonjour tout le monde !
Je réalise un petit projet avec symfony 4 pour mettre en relation des étudiants et des entreprises. J'aimerai récupérer les candidatures des offres pour les affichées à l'employer qui à créé l'offre. On considère que les étudiants et les employeurs sont des users et rien d'autre, une table admin sera prochainement créé pour attribuer les rôles pour ensuite sécurisé les routes et les views avec twig.
Voici mon modèle :
Pièce jointe 517058
Mes 2 entitées Candidature et Offre :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CandidatureRepository")
*/
class Candidature
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="candidatures")
* @ORM\JoinColumn(nullable=false)
*/
private $student;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Offer", inversedBy="candidatures")
* @ORM\JoinColumn(nullable=false)
*/
private $offer;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $coverLetter;
/**
* @ORM\Column(type="string", length=255)
*/
private $cvFileName;
public function getId(): ?int
{
return $this->id;
}
public function getStudent(): ?User
{
return $this->student;
}
public function setStudent(?User $student): self
{
$this->student = $student;
return $this;
}
public function getOffer(): ?Offer
{
return $this->offer;
}
public function setOffer(?Offer $offer): self
{
$this->offer = $offer;
return $this;
}
public function getCoverLetter(): ?string
{
return $this->coverLetter;
}
public function setCoverLetter(?string $coverLetter): self
{
$this->coverLetter = $coverLetter;
return $this;
}
public function getCvFileName(): ?string
{
return $this->cvFileName;
}
public function setCvFileName(string $cvFileName): self
{
$this->cvFileName = $cvFileName;
return $this;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
<?php
namespace App\Entity;
use Cocur\Slugify\Slugify;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\OfferRepository")
* @ORM\HasLifecycleCallbacks
*/
class Offer
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(max=255, maxMessage="Le titre ne peut pas faire plus de 255 caractères")
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="offers")
* @ORM\JoinColumn(nullable=false)
*/
private $author;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Candidature", mappedBy="offer")
*/
private $candidatures;
public function __construct()
{
$this->candidatures = new ArrayCollection();
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function initializeSlug() {
if(empty($this->slug)) {
$slugify = new Slugify();
$this->slug = $slugify->slugify($this->title);
}
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
/**
* @return Collection|Candidature[]
*/
public function getCandidatures(): Collection
{
return $this->candidatures;
}
public function addCandidature(Candidature $candidature): self
{
if (!$this->candidatures->contains($candidature)) {
$this->candidatures[] = $candidature;
$candidature->setOffer($this);
}
return $this;
}
public function removeCandidature(Candidature $candidature): self
{
if ($this->candidatures->contains($candidature)) {
$this->candidatures->removeElement($candidature);
// set the owning side to null (unless already changed)
if ($candidature->getOffer() === $this) {
$candidature->setOffer(null);
}
}
return $this;
}
} |