List Filters : afe_daterange_picker on a DateTime field

67 views
Skip to first unread message

David Z

unread,
Mar 11, 2014, 6:54:03 AM3/11/14
to symfony2adm...@googlegroups.com
Hello,

I want to filter my list on a "createdAt" DateTime field, using the "afe_daterange_picker" FormType, in order to be able to filter only elements which have the createdAt attribute between the two chosen dates.

Here's the declaration of my field in the Entity related to the List :

class myEntity{
     ....
     /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime")
     */
    private $createdAt;
}

and here's my declaration on generator.yml file :

params:
   .....
    fields:
        createdAt:
            formType: afe_daterange_picker
            addFormOptions:
                format: "dd/MM/yyyy HH:mm"

The display of the field is okay, but when submitted, it doesn't seem to work, it returns me all the results instead of only the ones with a createdAt value included in the range i chosen.
When the form is submitted, the posted filter value is like this :

Did I forgot something to have this working? I wonder if the mapping the posted daterange string and the value in my "$createdAt" field on my entity  is normally done automatically or if have to add something to get it work

Thanks for your help ,

David

Cédric Lombardot

unread,
Mar 11, 2014, 6:58:42 AM3/11/14
to Symfony2 Admingenerator bundles support for users
Hello,

Have you checked in your cache what is generated in the listcontroller ?


--
--
You received this message because you are subscribed to the Google
Groups "Symfony2 Admingenerator bundles support for users" group.
To post to this group, send email to
symfony2adm...@googlegroups.com
To unsubscribe from this group, send email to
symfony2admingene...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony2admingenerator?hl=en
 
---
Found bundle documentation at http://symfony2admingenerator.org/

---
You received this message because you are subscribed to the Google Groups "Symfony2 Admingenerator bundles support for users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to symfony2admingene...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

David Z

unread,
Mar 11, 2014, 7:10:43 AM3/11/14
to symfony2adm...@googlegroups.com
In the generated ListController I have this on the processFilters method :

   protected function processFilters($query)
    {
        $filterObject = $this->getFilters();

        $queryFilter = $this->getQueryFilter();
        $queryFilter->setQuery($query);

                    if (isset($filterObject['createdAt']) && null !== $filterObject['createdAt']) {
                $queryFilter->addDatetimeFilter('createdAt', $filterObject['createdAt']);

For more options, visit this group at
http://groups.google.com/group/symfony2admingenerator?hl=en
 
---
Found bundle documentation at http://symfony2admingenerator.org/

---
You received this message because you are subscribed to the Google Groups "Symfony2 Admingenerator bundles support for users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to symfony2admingenerator+unsub...@googlegroups.com.

Cédric Lombardot

unread,
Mar 11, 2014, 7:32:45 AM3/11/14
to Symfony2 Admingenerator bundles support for users
Is the $filterObject['createdAt'] "2014-03-11 - 2014-03-11" or is it an array ? addDateTimeFilter wait an array with 2 keys from and to https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/QueryFilter/DoctrineQueryFilter.php#L53

and the format of date should be :  'Y-m-d H:i:s' and not Y-m-d



For more options, visit this group at
http://groups.google.com/group/symfony2admingenerator?hl=en
 
---
Found bundle documentation at http://symfony2admingenerator.org/

---
You received this message because you are subscribed to the Google Groups "Symfony2 Admingenerator bundles support for users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to symfony2admingene...@googlegroups.com.

David Z

unread,
Mar 11, 2014, 7:58:15 AM3/11/14
to symfony2adm...@googlegroups.com
Yes the value in $filterObject['createdAt'] is actually a string "2014-04-06 - 2014-04-07"

If i set the option "use_daterange_entity" to true  in the generator.yml file :
        createdAt:
            formType: afe_daterange_picker
            addFormOptions:
                use_daterange_entity: true

$filterObject['
createdAt'] value then becomes a DateRangeObject :

'createdAt' => 
    object(Avocode\FormExtensionsBundle\Form\Model\DateRange)[932]
      private 'from' => 
        object(DateTime)[752]
          public 'date' => string '2014-04-06 00:00:00' (length=19)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Berlin' (length=13)
      private 'to' => 
        object(DateTime)[929]
          public 'date' => string '2014-04-07 00:00:00' (length=19)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Berlin' (length=13)

But even with this option it return all the results.

So if I use the
use_daterange_entity option, I have to override my ListConstroller to transform the DateRange object into an array in my $filterObject['createdAt'] variable before calling
$queryFilter->addDatetimeFilter method that's it?

By the way thank you very much for your responses it's a great help !

Cédric Lombardot

unread,
Mar 11, 2014, 9:22:07 AM3/11/14
to Symfony2 Admingenerator bundles support for users

As you can see here https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle/blob/master/QueryFilter/DoctrineQueryFilter.php#L55 admingenerator wait an array. 


I think the ArrayToStringTransformer should do the reverse transform to make an array with from and to https://github.com/avocode/FormExtensions/blob/master/Form/DataTransformer/ArrayToStringTransformer.php#L62

So you've not to use entity

Perheaps you can ask to @loostro why whe have this strange restult posting an issue on FormExtension repositiory

Cedric


David Z

unread,
Mar 11, 2014, 10:45:19 AM3/11/14
to symfony2adm...@googlegroups.com
I figured out i didn't have an up to date version of avocode... in my version it returned a simple string instead of an array/
After a composer update now it works perfectly !

However thanks for your help which made me understand a bit more DataTransformers !

David

For more options, visit this group at
http://groups.google.com/group/symfony2admingenerator?hl=en
 
---
Found bundle documentation at http://symfony2admingenerator.org/

---
You received this message because you are subscribed to the Google Groups "Symfony2 Admingenerator bundles support for users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to symfony2admingenerator+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages