首页 > 解决方案 > 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;
    }

标签: stringsymfonydatetimecollectionstwig

解决方案


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

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

现在,在 twig 中,如果你只想显示最后的certificate.purchase属性,你可以使用lastfilter :

{# 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 }}

推荐阅读