简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM