Récupérer les valeurs de variables d'une entité dans une autre

Par Anouchka25, il y a 6 ans


Bonjour,

J'aimerais récupérer les valeurs de variables d'une entité dans une autre. Je m'explique:

J'ai en premier fait un toString() sur la propriété name dans Company et ça marche mais je ne sais pas comment rajouter une seconde propriété.
Voici:

public function __toString() { return $this->name; }

6 réponses

bidule, il y a 6 ans

Salut,

Dans un premier temps as-tu une relation entre tes deux entités ? Si oui de quel type ?
Car dans l'absolu c'est juste un un getter à utiliser dans ta vue du style :

{# afficher le nom de la compagnie #} {{ job.company.name }}

nota: Ajoute tes deux entités pour que nous puissions voir.

Anouchka25, il y a 6 ans
{{ job.company.name }}

marche et j'arrive à récupérer le name de la company mais je veux aussi récupérer le logo et j'ai mis

{{ job.company.logo }}

et là ça ne marche pas.

ok, voici mon entité Job.php

<?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\JobRepository") */ class Job { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $title; /** * @ORM\Column(type="string", length=255) */ private $slug; /** * @ORM\Column(type="string", length=255) */ private $summary; /** * @ORM\Column(type="string", length=255) */ private $content; /** * @ORM\Column(type="datetime") */ private $publishedAt; /** * @ORM\Column(type="integer") */ private $salary; /** * @ORM\Column(type="string", length=255) */ private $adresse; /** * @ORM\OneToOne(targetEntity="App\Entity\Company", inversedBy="job", cascade={"persist", "remove"}) */ private $company; /** * @ORM\Column(type="string", length=30, nullable=true) */ private $contrat; 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 getSummary(): ?string { return $this->summary; } public function setSummary(string $summary): self { $this->summary = $summary; return $this; } public function getContent(): ?string { return $this->content; } public function setContent(string $content): self { $this->content = $content; return $this; } public function getPublishedAt(): ?\DateTimeInterface { return $this->publishedAt; } public function setPublishedAt(\DateTimeInterface $publishedAt): self { $this->publishedAt = $publishedAt; return $this; } public function getSalary(): ?int { return $this->salary; } public function setSalary(int $salary): self { $this->salary = $salary; return $this; } public function getAdresse(): ?string { return $this->adresse; } public function setAdresse(string $adresse): self { $this->adresse = $adresse; return $this; } public function getCompany(): ?Company { return $this->company; } public function setCompany(?Company $company): self { $this->company = $company; return $this; } public function getContrat(): ?string { return $this->contrat; } public function setContrat(?string $contrat): self { $this->contrat = $contrat; return $this; } }

Mon entitit Company.php

<?php namespace App\Entity; 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\CompanyRepository") */ class Company { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $name; /** * @ORM\Column(type="string", length=255) */ private $summary; /** * @ORM\Column(type="string", length=255) */ private $website; /** * @ORM\Column(type="string", length=255) */ private $email; /** * @ORM\Column(type="datetime") */ private $datecreation; /** * @ORM\Column(type="string", length=255) */ private $adresse; /** * @ORM\Column(type="integer") */ private $employees; /** * @ORM\Column(type="string", length=255) */ private $facebook; /** * @ORM\Column(type="string", length=255) */ private $google; /** * @ORM\Column(type="string", length=255) */ private $twitter; /** * @ORM\OneToMany(targetEntity="App\Entity\Job", mappedBy="company") */ private $jobs; /** * @ORM\Column(type="string", length=255) */ private $instagram; /** * @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="company", cascade={"persist", "remove"}) */ private $user; /** * @ORM\OneToOne(targetEntity="App\Entity\Job", mappedBy="company", cascade={"persist", "remove"}) */ private $job; /** * @ORM\Column(type="string") * * @Assert\NotBlank(message="Veuillez télécharger votre logo(.jpg/.png).") * @Assert\File(mimeTypes={ "image/png", "image/jpeg" }) */ private $logo; public function __construct() { $this->jobs = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getName(): ?string { return $this->name; } public function setName(string $name): self { $this->name = $name; return $this; } public function getSummary(): ?string { return $this->summary; } public function setSummary(string $summary): self { $this->summary = $summary; return $this; } public function getWebsite(): ?string { return $this->website; } public function setWebsite(string $website): self { $this->website = $website; return $this; } public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): self { $this->email = $email; return $this; } public function getDatecreation(): ?\DateTimeInterface { return $this->datecreation; } public function setDatecreation(\DateTimeInterface $datecreation): self { $this->datecreation = $datecreation; return $this; } public function getAdresse(): ?string { return $this->adresse; } public function setAdresse(string $adresse): self { $this->adresse = $adresse; return $this; } public function getEmployees(): ?int { return $this->employees; } public function setEmployees(int $employees): self { $this->employees = $employees; return $this; } public function getFacebook(): ?string { return $this->facebook; } public function setFacebook(string $facebook): self { $this->facebook = $facebook; return $this; } public function getGoogle(): ?string { return $this->google; } public function setGoogle(string $google): self { $this->google = $google; return $this; } public function getTwitter(): ?string { return $this->twitter; } public function setTwitter(string $twitter): self { $this->twitter = $twitter; return $this; } /** * @return Collection|Job[] */ public function getJobs(): Collection { return $this->jobs; } public function addJob(Job $job): self { if (!$this->jobs->contains($job)) { $this->jobs[] = $job; $job->setCompany($this); } return $this; } public function removeJob(Job $job): self { if ($this->jobs->contains($job)) { $this->jobs->removeElement($job); // set the owning side to null (unless already changed) if ($job->getCompany() === $this) { $job->setCompany(null); } } return $this; } public function getInstagram(): ?string { return $this->instagram; } public function setInstagram(string $instagram): self { $this->instagram = $instagram; return $this; } public function getUser(): ?User { return $this->user; } public function setUser(?User $user): self { $this->user = $user; return $this; } public function getJob(): ?Job { return $this->job; } public function setJob(?Job $job): self { $this->job = $job; // set (or unset) the owning side of the relation if necessary $newCompany = $job === null ? null : $this; if ($newCompany !== $job->getCompany()) { $job->setCompany($newCompany); } return $this; } public function __toString() { return $this->name; } public function getLogo(): ?string { return $this->logo; } public function setLogo(string $logo): self { $this->logo = $logo; return $this; } }
bidule, il y a 6 ans

pour les images perso j'ai activé la copie des images directement dans le dossier "/public/images/"

1- Dans webpack.config.js:
j'ai ajouté ceci, afin de copier les images directement mais sans le verssionning.

// public path used by the web server to access the output path .copyFiles({ from: './assets/images', to: 'images/[path][name].[ext]' })

2- Puis quand tu fais un

yarn build

cela permet de copier les images du dossier "/assets/images" vers "/public/images"

3- Dans ma vue Twig

{{ asset('public/images/' ~ job.company.getLogo()) }}

cela devrait fonctionner ;)

Anouchka25, il y a 6 ans

Je crois avoir suivi ta démarche mais j'ai cette erreur :
An exception has been thrown during the rendering of a template ("Notice: Undefined index: company").
A la ligne :

<img src="{{ asset('public/images/' ~ job.company.getLogo()) }}" class="img-responsive" alt="" />
Anouchka25, il y a 6 ans

Attends je crois que c'est moi qui n'a d'image stocké

jelekin, il y a 6 ans

ça a l'air bien pourtant... Si tu regardes la source générée, ça a le bon format myloweslife