string - Twig,显示日期时间集合?
问题描述
我有 2 个实体,证书和购买。在树枝中,我有一个表格,我在其中按行显示每个证书。我真的不明白如何显示最后一个 certificate.purchase.start/end。Symfony 版本: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;
}
}
和采购实体:
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;
}
}
我在证书中有其他集合,但我很容易用 __toString() 将它们放在树枝中。就像在我的工作实体中一样:
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;
}
解决方案
我假设您在视图中传递了一个对象certificate
:
// in your controller
return $this->render('theview.html.twig',
array('certificate' => $certificate));
现在,在 twig 中,如果你只想显示最后的certificate.purchase
属性,你可以使用last
filter :
{# set a variable containing the last purchase #}
{% set lastestPurchase = certificate.purchase|last %}
last certificate start : {{ lastestPurchase.start }}
last certificate end : {{ lastestPurchase.end }}
或者,如果您不需要在视图中传递整个集合,您可以直接在控制器中获取最后一个元素:
// 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));
然后,在视图中:
last certificate start : {{ lastestPurchase.start }}
last certificate end : {{ lastestPurchase.end }}
推荐阅读
- c++ - 如何在返回类型函数模板的特化中使用派生类型?(“无法推断模板参数”)
- python-3.x - 如何让 Pytest 在 python 脚本中运行特定的测试?
- amazon-web-services - 如何使用气流将数据从 redshift 导出到 s3?
- nginx - 如何在磁盘上查找与正则表达式匹配的文件?(例如 /123-foo 提供 /123-bar.html)
- android - 尝试在模拟器上安装第二个颤振应用程序时出错
- c++ - understand the pointer pointer in c++ to get the function call
- html - 如何在 GitHub README.md 中显示 Codecov.io 徽章?
- ios - 在导航栏下隐藏 UITableView
- javascript - 如何在数组中记录所有异步函数的结果
- javascript - 如何在标签 Highcharts 中设置“Y”位置