Sonata Admin Filters中的日期范围选择器

时间:2015-02-24 04:49:51

标签: symfony sonata-admin

以前,我们可以做类似的事情:

 ->add('workDate', 'doctrine_orm_date_range', array(), null, array('format' => 'dd-MM-yyyy', 'widget' => 'single_text', 'attr' => array('class' => 'datepicker')));

你会得到一个日期选择器。但这破坏并继续得到以下错误

The options "format", "widget" do not exist. Known options are: "action", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "field_options", "field_type", "horizontal_input_wrapper_class", "horizontal_label_class", "horizontal_label_offset_class", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_render", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "sonata_admin", "sonata_field_description", "sonata_help", "translation_domain", "trim", "validation_groups", "virtual" 

任何人都知道如何更新此代码以使用新版Sonata?

2 个答案:

答案 0 :(得分:2)

由于最新版本的奏鸣曲已经安装了doctrine_orm_datetime_range,我遇到了与"sonata-project/admin-bundle": "dev-master"相同的问题,但我无法将doctrine_orm_datetime_range渲染为文字字段,然后又恢复了我的composer.json到2.3版本的奏鸣曲管理员包,我能够将日期时间范围类型呈现为文本

->add(
    'updatedAt',
    'doctrine_orm_datetime_range',
    array('label' => 'Updated'),
    null,
    array(
        'widget' => 'single_text',
        'format' => 'yyyy-MM-dd HH:mm:ss',
        'required' => false,
        'attr' => array('class' => 'datetimepicker')
    )
)

composer.json

"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.6.*",
    "doctrine/orm": "~2.2,>=2.2.3,<2.5",
    "doctrine/dbal": "<2.5",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~3.0,>=3.0.12",
    "sensio/framework-extra-bundle": "~3.0,>=3.0.2",
    "raulfraile/ladybug-bundle": "~1.0",
    "jms/serializer-bundle": "dev-master",
    "phpdocumentor/reflection-docblock": "dev-master",
    "jms/security-extra-bundle": "dev-master",
    "sonata-project/admin-bundle": "2.3",
    "sonata-project/doctrine-orm-admin-bundle": "dev-master",
    "sonata-project/core-bundle": "~2.2@dev",
    "sonata-project/user-bundle": "dev-master",
    "sonata-project/datagrid-bundle": "~2.2@dev",
    "egeloen/ckeditor-bundle": "~2.2",
    "sonata-project/formatter-bundle": "~2.3",
    "sonata-project/news-bundle": "~2.3",
    "sonata-project/classification-bundle": "~2.2",
    "sonata-project/seo-bundle": "~1.1",
    "liip/imagine-bundle": "~1.2",
    "sonata-project/cache-bundle": "~2.2",
    "sonata-project/page-bundle": "~2.3",
    "sonata-project/notification-bundle": "~2.3",
    "symfony-cmf/routing-bundle": "~1.3",
    "sonata-project/intl-bundle": "~2.2",
    "jms/translation-bundle": "~1.1",
    "friendsofsymfony/jsrouting-bundle": "~1.5"

}

但是恢复到以前的版本,你将失去奏鸣曲的新功能,例如管理员的网格视图

答案 1 :(得分:2)

使用doctrine_orm_callback过滤器类型的Sonata admin 2.4 的另一种方法,例如:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $now  = new \DateTime();

    $datagridMapper
        ->add('dateReport', 'doctrine_orm_callback',  array(
                'label' => 'Date',
                'callback' => function($queryBuilder, $alias, $field, $value) {
                    if (empty($value['value'])) { return; }

                    $date = $value['value'];
                    $queryBuilder->andWhere($alias.'.dateReport BETWEEN :start AND :end');
                    $queryBuilder->setParameter('start', $date->start);
                    $queryBuilder->setParameter('end', $date->end);

                    return true;
                },
                'field_type' => 'date_range',
                'field_options' => array(
                    'start_options' => array(
                        'years'           => range(2014, $now->format('Y')),
                        'dp_min_date'     => '12/1/2014',
                        'dp_max_date'     => $now->format('m/d/Y'),
                        'dp_default_date' => $now->format('m/d/Y'),
                        'dp_language'     => 'fr',
                        ),
                    'end_options' => array(
                        'years'           => range(2014, $now->format('Y')),
                        'dp_min_date'     => '12/1/2014',
                        'dp_max_date'     => $now->format('m/d/Y'),
                        'dp_default_date' => $now->format('m/d/Y'),
                        'dp_language'     => 'fr',
                        )
                    )
                )
            )

    ;
}

start_optionsend_options数组中的field_options尚未记录