iVela
iVela

Reputation: 1201

How to get just one item of related entity in Twig

I have "Person" entity that has a property "Status" and this property is an OneToMany relationship in Doctrine.

/**
 * 
 * @ORM\OneToMany(targetEntity="\My\Bundle\Entity\Status", mappedBy="person")
 * 
 **/
protected $status;

What I need to do is to display in my view is just the last status.

How can I get just the last status in my twig view? Is there something like, for exmample, {{ person.status.last }} ?

Or should I query the last status in my controller and pass it to view as another var?

Upvotes: 0

Views: 270

Answers (1)

pazulx
pazulx

Reputation: 2379

Yes, you can do it exactly like this {{ person.status.last.someField }} to echo a someField property of last status (in natural query order) for person object.

This is possible because person.status is a Doctrine Collection which has methods like first or last. You can check this for more information.

Upvotes: 1

Related Questions