Twig,显示日期时间集合?

Posted

技术标签:

【中文标题】Twig,显示日期时间集合?【英文标题】:Twig, display datetime collection? 【发布时间】:2021-11-20 09:46:51 【问题描述】:

我有 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;
    

【问题讨论】:

【参考方案1】:

我假设你在视图中传递了一个对象 certificate

// in your controller
return $this->render('theview.html.twig',
            array('certificate' => $certificate));

现在,在 twig 中,如果您只想显示最后的 certificate.purchase 属性,可以使用 last 过滤器:

# 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 

【讨论】:

谢谢!您的解决方案帮助了我,但我不得不添加一些代码,因为我对证书进行了分页: Controller: foreach ($certificates as $certificate) $lastestPurchases[] = $certificate->getPurchase()->last(); Twig: % for lastestPurchase in lastestPurchase % % if lastestPurchase.certificate.id == certificate.id % lastestPurchase.end|date('d F Y') % endif % % endfor %

以上是关于Twig,显示日期时间集合?的主要内容,如果未能解决你的问题,请参考以下文章

如何用twig和symfony在输入中显示日期?

Twig - 格式化日期

在Twig模板中使用4位数年份的本地化日期

如何检查今天是不是在 Twig 的两个日期之间?

Twig日期循环每一天在一个时期

如何在没有时区干扰的情况下在树枝中渲染时间