我是Symfony2的新手,并试图使用Lexik Filter套装;我有2个实体(父母培训)以下面定义的nm(多对多)关系链接,我试图按名称过滤父母列表:`tuto \ LexikTestBundle \ Entity \父母:
type: entity
table: Parents
repositoryClass: tuto\LexikTestBundle\Repository\ParentRepository
id:
id:
type: integer
generator:
strategy: AUTO
fields:
Firstname:
type: string
length: 50
Lastname:
type: string
length: 50
DOB:
type: datetime
Email:
type: string
length: 50
manyToMany:
Trainings:
targetEntity: Training
mappedBy: parents`
我遵循使用ver 3.0.8的教程并定义了以下自定义过滤器类型:
namespace tuto\LexikTestBundle\Form\Filter;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MyParentsType extends AbstractType
{
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'parents_filter';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Firstname', 'filter_text');
$builder->add('Lastname', 'filter_text');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
[
'csrf_protection' => false,
'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message
]
);
}
}
控制器中的以下FilterAction:
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('tutoLexikTestBundle:Parents')->findAll();
$form = $this->testFilterAction($request);
return $this->render('tutoLexikTestBundle:Parents:index.html.twig', [
'entities' => $entities,
'form' => $form,
]
);
}
public function testFilterAction(Request $request)
{
$form= $this->get('form.factory')->create(new MyParentsType());
if($request->query->has($form->getName())) {
// manually bind values from the request
$form->submit($request->query->get($form->getName()));
$queryBuilder = $this->get('doctrine.orm.entity_manager')
->getRepository('LexikTestBundle:Parents')
->createQueryBuilder('e');
//build the query from the given object
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form,$queryBuilder);
var_dump($queryBuilder->getDql());
}
return $this->render(
'tutoLexikTestBundle:Default:testFilter.html.twig',
['form'=>$form->createView()]
);
}
实施的树枝是
<form method="get" action=".">
{{ form_rest(form) }}
<input type="submit" name="submit-filter" value="filter" />
</form>
&#13;
我已经通过以下答案,似乎没有相同的错误:
但是我仍然面临 Catchable Fatal Error:传递给Symfony \ Component \ Form \ FormRenderer :: searchAndRenderBlock()的参数1必须是Symfony \ Component \ Form \ FormView的实例,实例Symfony \ Component \ HttpFoundation \响应任何提示都是受欢迎的。
答案 0 :(得分:1)
您应该呈现表单视图而不是表单对象
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('tutoLexikTestBundle:Parents')->findAll();
$form = $this->testFilterAction($request);
return $this->render('tutoLexikTestBundle:Parents:index.html.twig', [
'entities' => $entities,
'form' => $form->createView(),
]
);
}
testFilterAction返回一个Response对象而不是表单对象或视图 请按以下方式更改您的方法:
注意:您可以在
中嵌套表单模板tutoLexikTestBundle:Parents:index.html.twig' view
public function testFilterAction(Request $request)
{
$form= $this->get('form.factory')->create(new MyParentsType());
if($request->query->has($form->getName())) {
// manually bind values from the request
$form->submit($request->query->get($form->getName()));
$queryBuilder = $this->get('doctrine.orm.entity_manager')
->getRepository('LexikTestBundle:Parents')
->createQueryBuilder('e');
//build the query from the given object
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form,$queryBuilder);
var_dump($queryBuilder->getDql());
}
return $form; // return form object
);
}