我尝试在[Symfony-Book] [1]
的帮助下努力生成基于用户数据的表单表单出现,但数据不是基于查询构建器,我不知道,我哪里出错了。公式显示实体的所有对象,但不显示在querybuilder中定义的限制
我的表格类型:
<?php
namespace Pso\ProjectBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Doctrine\ORM\EntityRepository;
class ProjectuserType extends AbstractType
{
private $id;
public function __construct($id)
{$this->id = $id;}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$id = $this->id;
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($id) {
$form = $event->getForm();
$formOptions = array(
'class' => 'Pso\LogBundle\Entity\User',
'property' => 'username',
'multiple' => 'true',
'expanded' => 'true',
'query_builder' => function (EntityRepository $er) use($id) {
return $er->createQueryBuilder('x')->FROM('PsoProjectBundle:project', 'p')
->JOIN('p.users','u')
->WHERE('p.id <>:id')
->setParameter('id', $id)
;
},
);
$form->add('user', 'entity', $formOptions)
->add('Submit', 'submit', array(
'attr'=> array (
'formnovalidate' => 'formnovalidate'
)
));
}
);
}
public function getName()
{
return 'Projectuser';
}
}
更新:
现在我尝试在构建器中更改我的查询,使其看起来:
<?php
namespace Pso\ProjectBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Doctrine\ORM\EntityRepository;
class ProjectuserType extends AbstractType
{
private $id;
public function __construct($id)
{
$this->id = $id;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$id = $this->id;
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($id) {
$form = $event->getForm();
$formOptions = array(
'class' => 'Pso\LogBundle\Entity\User',
'property' => 'username',
'multiple' => 'true',
'expanded' => 'true',
'query_builder' => function (EntityRepository $er) use($id) {
return $er->createQueryBuilder('x')->SELECT('u')
->FROM('PsoLogBundle:user', 'u')
->JOIN('PsoProjectBundle:project', 'p')
->WHERE('p.id <>:id')
->setParameter('id', $id)
;
},
);
$form->add('user', 'entity', $formOptions)
->add('Submit', 'submit', array(
'attr'=> array (
'formnovalidate' => 'formnovalidate'
)
));
}
);
}
public function getName()
{
return 'Projectuser';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Pso\LogBundle\Entity\User',
));
}
}
在这种情况下,我收到错误
[语法错误]第0行,第79行:错误:预期的Doctrine \ ORM \ Query \ Lexer :: T_WITH,得到','
更新:
控制器:
$form = $this->createFormBuilder()
->add('projectuser', ['except_id' => $id])
->getForm();
$form->handleRequest($request);
$build['form'] = $form->createView();
return $this->render('PsoProjectBundle:Project:editProject.html.twig', array('projects' =>$projects, 'users' =>$users, 'form' => $form->createView(), ));
ProjectuserType.php:
<?php
class ProjectuserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
\assert(isset($options['except_id']));
$exceptId = $options['except_id'];
$formOptions = [
'class' => User::class, // PHP5.5
'property' => 'username',
'multiple' => 'true',
'expanded' => 'true',
'query_builder' => function (EntityRepository $er) use($exceptId) {
return $er
->createQueryBuilder('u')
->join('u.project', 'p')
->where('p.id <> :id')
->setParameter('id', $exceptId)
;
}
];
$submitOptions = [
'attr'=> [
'formnovalidate' => 'formnovalidate'
]
];
$builder
->add('user', 'entity', $formOptions)
->add('Submit', 'submit', $submitOptions)
;
}
public function getName()
{
return 'Projectuser';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => FormUser::class, // that line is wrong in your code! it must be a class with attributes 'user' and 'submit', see the "builder->add" calls
]);
$resolver->setRequired(['except_id']);
}
}
答案 0 :(得分:0)
FormTypes应该有default-ctors或service-parameters,因为您可以将它们注册为服务,以便能够创建表单,如:
<?php
// in the controller action (didn't test it)
$form = $this->createFormBuilder($dataObject)
->add('projectuser', [
'except_id' => $yourId
])
->getForm();
这不是因为它更容易编写,但您不想处理如何创建Type。如果您的类型取决于其他服务,您不想更改您的应用程序,只需编辑services.yml。
因此,请通过options参数传递您的id,并配置ProjectuserType以允许该选项。删除该事件监听器。我构建了最疯狂的形式,但从未需要这些事件。
//编辑试试这个:
<?php
class ProjectuserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
\assert(isset($options['except_id']));
$exceptId = $options['except_id'];
$formOptions = [
'class' => User::class, // PHP5.5
'property' => 'username',
'multiple' => 'true',
'expanded' => 'true',
'query_builder' => function (EntityRepository $er) use($exceptId) {
return $er
->createQueryBuilder('u')
->join('u.project', 'p')
->where('p.id <> :id')
->setParameter('id', $exceptId)
;
}
];
$submitOptions = [
'attr'=> [
'formnovalidate' => 'formnovalidate'
]
];
$builder
->add('user', 'entity', $formOptions)
->add('Submit', 'submit', $submitOptions)
;
}
public function getName()
{
return 'Projectuser';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => FormUser::class, // that line is wrong in your code! it must be a class with attributes 'user' and 'submit', see the "builder->add" calls
]);
$resolver->setRequired(['except_id']);
}
}