我正在尝试使用Lexik Form Filter包按发布日期过滤文章列表。
我的实体:
class Article {
[...]
/**
* @ORM\Column(name="publish_date", type="datetime", nullable=true)
*/
protected $publishDate;
[...]
}
我的表单类型:
class ArticleFilterType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('publishDate', 'filter_date');
[...]
}
public function getName()
{
return 'article_filter';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Orm\CrawlerBundle\Entity\Article',
'csrf_protection' => false,
'validation_groups' => array('filter'),
));
}
当我使用我的某篇文章的发布日期过滤填写表单时,我没有结果。问题是什么?我在db中使用日期的类型是datetime。
谢谢!
答案 0 :(得分:1)
我猜你应该使用Lexik的filter_datetime或filter_datetime_range:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('publishDate', 'filter_datetime');
[...]
}
答案 1 :(得分:0)
控制器:
class ArticleController extends Controller {
/**
* Lists all Article entities.
*
* @Route("/article_admin", name="article_admin")
* @Template()
*/
public function listArticleAction() {
/** @var ArticleBiz $em */
$em = $this->get('orm_crawler.articlebiz');
$form = $this->createForm(new ArticleFilterType());
// initialize a query builder
/** @var QueryBuilder $filterBuilder */
$filterBuilder = $em->getRepository()->createQueryBuilder('e');
if ($this->get('request')->query->has($form->getName())) {
// manually bind values from the request
$form->submit($this->get('request')->query->get($form->getName()));
// build the query from the given form object
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder);
}
$paginator = $this->get('knp_paginator')->paginate($filterBuilder->getQuery(), $this->getRequest()->query->get('page', 1), 10);
return array(
'form' => $form->createView(),
'paginator' => $paginator
);
}