Symfony 4 : doctrine, relationships and twig


Symfony 4 : doctrine, relationships and twig



I'm currently working on my first symfony 4 project and I'm having a problem with my twig templating.
First of all here is a UML diagram of my created objects.



UML



And my layout should look sorta like this :



Layout



Here are my entities :



Region


<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
* @ORMEntity(repositoryClass="AppRepositoryRegionRepository")
*/
class Region
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $designation;

/**
* @ORMOneToMany(targetEntity="AppEntityCountry", mappedBy="region", orphanRemoval=true)
*/
private $countries;

/**
* @ORMColumn(type="string", length=255)
*/
private $initials;

public function __construct()
{
$this->countries = new ArrayCollection();
}

public function getId()
{
return $this->id;
}

public function getDesignation(): ?string
{
return $this->designation;
}

public function setDesignation(string $designation): self
{
$this->designation = $designation;

return $this;
}

/**
* @return Collection|Country
*/
public function getCountries(): Collection
{
return $this->countries;
}

public function addCountry(Country $country): self
{
if (!$this->countries->contains($country)) {
$this->countries = $country;
$country->setRegion($this);
}

return $this;
}

public function removeCountry(Country $country): self
{
if ($this->countries->contains($country)) {
$this->countries->removeElement($country);
// set the owning side to null (unless already changed)
if ($country->getRegion() === $this) {
$country->setRegion(null);
}
}

return $this;
}

public function getInitials(): ?string
{
return $this->initials;
}

public function setInitials(string $initials): self
{
$this->initials = $initials;

return $this;
}

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


}



Continent


<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
* @ORMEntity(repositoryClass="AppRepositoryContinentRepository")
*/
class Continent
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $name;

/**
* @ORMColumn(type="string", length=255)
*/
private $code;

/**
* @ORMOneToMany(targetEntity="AppEntityCountry", mappedBy="continent", orphanRemoval=true)
*/
private $countries;

public function __construct()
{
$this->countries = new ArrayCollection();
}

public function getId()
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getCode(): ?string
{
return $this->code;
}

public function setCode(string $code): self
{
$this->code = $code;

return $this;
}

/**
* @return Collection|Country
*/
public function getCountries(): Collection
{
return $this->countries;
}

public function addCountry(Country $country): self
{
if (!$this->countries->contains($country)) {
$this->countries = $country;
$country->setContinent($this);
}

return $this;
}

public function removeCountry(Country $country): self
{
if ($this->countries->contains($country)) {
$this->countries->removeElement($country);
// set the owning side to null (unless already changed)
if ($country->getContinent() === $this) {
$country->setContinent(null);
}
}

return $this;
}

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



Country


<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
* @ORMEntity(repositoryClass="AppRepositoryCountryRepository")
*/
class Country
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="string", length=255)
*/
private $name;

/**
* @ORMColumn(type="string", length=255)
*/
private $iso;

/**
* @ORMManyToOne(targetEntity="AppEntityRegion", inversedBy="countries")
* @ORMJoinColumn(name="region_id", referencedColumnName="id")
*/
private $region;

/**
* @ORMOneToMany(targetEntity="AppEntityDocument", mappedBy="country", orphanRemoval=true)
*/
private $documents;

/**
* @ORMManyToOne(targetEntity="AppEntityContinent", inversedBy="countries")
* @ORMJoinColumn(name="continent_id", referencedColumnName="id")
*/
private $continent;

public function __construct()
{
$this->documents = new ArrayCollection();
}

public function getId()
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getIso(): ?string
{
return $this->iso;
}

public function setIso(string $iso): self
{
$this->iso = $iso;

return $this;
}

public function getRegion(): ?Region
{
return $this->region;
}

public function setRegion(?Region $region): self
{
$this->region = $region;

return $this;
}

/**
* @return Collection|Document
*/
public function getDocuments(): Collection
{
return $this->documents;
}

public function addDocument(Document $document): self
{
if (!$this->documents->contains($document)) {
$this->documents = $document;
$document->setCountry($this);
}

return $this;
}

public function removeDocument(Document $document): self
{
if ($this->documents->contains($document)) {
$this->documents->removeElement($document);
// set the owning side to null (unless already changed)
if ($document->getCountry() === $this) {
$document->setCountry(null);
}
}

return $this;
}

public function getContinent(): ?Continent
{
return $this->continent;
}

public function setContinent(?Continent $continent): self
{
$this->continent = $continent;

return $this;
}
}



Document


<?php

namespace AppEntity;

use DoctrineORMMapping as ORM;
use VichUploaderBundleMappingAnnotation as Vich;
use SymfonyComponentHttpFoundationFileFile;

/**
* @ORMEntity(repositoryClass="AppRepositoryDocumentRepository")
* @VichUploadable()
*/
class Document
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMManyToOne(targetEntity="AppEntityCountry", inversedBy="documents")
* @ORMJoinColumn(name="country_id", referencedColumnName="id")
*/
private $country;

/**
* @ORMColumn(type="string")
*/
private $level;

/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @VichUploadableField(mapping="country_document", fileNameProperty="documentName", size="documentSize")
*
* @var File
*/
private $documentFile;

/**
* @ORMColumn(type="string", length=255)
*
* @var string
*/
private $documentName;

/**
* @ORMColumn(type="integer")
*
* @var integer
*/
private $documentSize;

/**
* @ORMColumn(type="datetime")
*
* @var DateTime
*/
private $updatedAt;

public function getId()
{
return $this->id;
}

public function getCountry(): ?Country
{
return $this->country;
}

public function setCountry(?Country $country): self
{
$this->country = $country;

return $this;
}

public function getLevel(): ?int
{
return $this->level;
}

public function setLevel(int $level): self
{
$this->level = $level;

return $this;
}

public function setDocumentFile(?File $document = null): void
{
$this->documentFile = $document;

if (null !== $document) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new DateTimeImmutable();
}
}

public function getDocumentFile(): ?File
{
return $this->documentFile;
}

public function setDocumentName(?string $documentName): void
{
$this->documentName = $documentName;
}

public function getDocumentName(): ?string
{
return $this->documentName;
}

public function setDocumentSize(?int $documentSize): void
{
$this->documentSize = $documentSize;
}

public function getDocumentSize(): ?int
{
return $this->documentSize;
}

}



I have 4 regions. These regions each have multiple countries who are affiliated to a continent and have 2 documents attributed to them.
How should I proceed to get the layout I want ?





Too broad, we don't know how your entities are connected codewise
– DarkBee
Jun 29 at 7:21





Yeah I realized that this morning. Entities code added !
– Orinthia
Jun 29 at 8:40









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift