httpete
httpete

Reputation: 6142

Symfony2 - Use a personal function in an Entity

I have an entity called Photo. A photo has a date field and I would like to format that date to relative time (example: "2 minutes ago" instead of "2012-01-23 01:45:00"). I found a function to do that, now I would like to know where and how I should call this function ?

I thought I could do that directly in the getDate() of my entity. I created one service with my function to format date but then i realize that I can not access the "container" in an Entity. ($this->container->get('datehelper'); won't work.)

How should I process then ? Thank you.

Upvotes: 0

Views: 283

Answers (1)

Nanocom
Nanocom

Reputation: 3726

The bundle you want: KnpTimeBundle.

If you want your own implementation, create a dedicated bundle (or insert it in a general bundle like "CommonBundle"), create your DateTimeFormatter class, and create your twig extension. You can of course inspire yourself from the KnpTimeBundle.

Upvotes: 4

Related Questions