I have 2 entities, Certificate and purchase. In twig i have a table where i display each certificate in rows. I really don't get how to display the last certificate.purchase.start/end. Symfony version: 5.3.7
class Certificate
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity=Purchase::class, mappedBy="certificate")
*/
private $purchase;
public function __construct()
{
$this->job = new ArrayCollection();
$this->ticket = new ArrayCollection();
$this->purchase = new ArrayCollection();
}
/**
* @return Collection|Purchase[]
*/
public function getPurchase(): Collection
{
return $this->purchase;
}
public function addPurchase(Purchase $purchase): self
{
if (!$this->purchase->contains($purchase)) {
$this->purchase[] = $purchase;
$purchase->setCertificate($this);
}
return $this;
}
public function removePurchase(Purchase $purchase): self
{
if ($this->purchase->removeElement($purchase)) {
// set the owning side to null (unless already changed)
if ($purchase->getCertificate() === $this) {
$purchase->setCertificate(null);
}
}
return $this;
}
}
And Purchase entity:
class Purchase
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $start;
/**
* @ORM\Column(type="datetime")
*/
private $end;
/**
* @ORM\ManyToOne(targetEntity=Certificate::class, inversedBy="purchase")
* @ORM\JoinColumn(nullable=false)
*/
private $certificate;
public function getId(): ?int
{
return $this->id;
}
public function getStart(): ?\DateTimeInterface
{
return $this->start;
}
public function setStart(\DateTimeInterface $start): self
{
$this->start = $start;
return $this;
}
public function getEnd(): ?\DateTimeInterface
{
return $this->end;
}
public function setEnd(\DateTimeInterface $end): self
{
$this->end = $end;
return $this;
}
public function getCertificate(): ?Certificate
{
return $this->certificate;
}
public function setCertificate(?Certificate $certificate): self
{
$this->certificate = $certificate;
return $this;
}
}
I have other collections in certificate but i easly get them in twig with __toString(). Like in the job entity i have:
class Job
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $code;
/**
* @ORM\ManyToOne(targetEntity=Certificate::class, inversedBy="job")
* @ORM\JoinColumn(nullable=false)
*/
private $certificate;
public function __toString()
{
return $this->code;
}
CodePudding user response:
I assume you pass in the view an object certificate
:
// in your controller
return $this->render('theview.html.twig',
array('certificate' => $certificate));
Now, in twig, if you want to display only the last certificate.purchase
properties, you can use the last
filter :
{# set a variable containing the last purchase #}
{% set lastestPurchase = certificate.purchase|last %}
last certificate start : {{ lastestPurchase.start }}
last certificate end : {{ lastestPurchase.end }}
Alternatively, if you don't need to pass the whole collection in the view, you can get the last element directly in the controller :
// TODO : Check the return value of last(), if the collection is empty, it returns false
$lastestPurchase = $certificate->getPurchase()->last();
// ...
return $this->render('theview.html.twig',
array('lastestPurchase' => $lastestPurchase));
And then, in the view :
last certificate start : {{ lastestPurchase.start }}
last certificate end : {{ lastestPurchase.end }}