Use Unix timestamp in Doctrine Timestampable

前端 未结 3 1878
囚心锁ツ
囚心锁ツ 2021-02-01 11:14

How do I use Unix timestamps with the Doctrine Timestampable behavior? I found the following code snippet here, but I\'d rather not manually add this everywhere:



        
3条回答
  •  旧时难觅i
    2021-02-01 11:47

    One method would be to use doctorine's listeners to create a unix timestamp equivalent when the record is fetched and before it is saved:

    class Base extends Doctrine_Record_Listener
    {
        public function preHydrate(Doctrine_Event $event)
        {
            $data = $event->data;
    
            $data['unix_created_at'] = strtotime($data['created_at']);
            $data['unix_updated_at'] = strtotime($data['updated_at']);
    
            $event->data = $data;
        }
    }
    

    This could be your base class that you extend in anything that needs created_at and updated_at functionality.

    I'm sure with a little bit more tinkering you could loop through $data and convert all datetime fields 'unix_'.$field_name.

    Good luck

提交回复
热议问题