简体   繁体   中英

Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

So i have a GUI for a simple database made with Symfony2 + Sonata Admin Bundle + Doctrine 2. That database will hold billions of rows so the dates are stored as timestamps (to save space) but in the GUI displayed as dates (ex: 2013/10/17 10:05:06). Everything works in the GUI except the filtering by dates. I tried all sorts of configurations in the class that extends the Admin, method configureDatagridFilters(). I cannot make it to work... can you help me?

I found a work around that i think it can be useful in many other situations as well:

So in the class that extends Admin.php we have the method configureDatagridFilters(). There one can add an input like

        ->add('startDateFrom', 'doctrine_orm_callback', array(
            'label' => 'Start Date from',
            'callback' => function($queryBuilder, $alias, $field, $value) {
        if (!$value['value']) {
            return;
        }
        $inputValue = $this->dateStringToTimestamp($value['value']);
        $queryBuilder->andWhere("{$alias}.startDate >= :startDateFrom");
        $queryBuilder->setParameter('startDateFrom', $inputValue);
        return true;
    },
            'field_type' => 'text'
                ), null, array('attr' => array('class' => 'calendar',
    )))

As you can see, in this way we can manipulate the field value as we wish. I hope it will help others too :)

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