Symfony2 实体表单中的查询生成器
Query Builder inside Symfony2 Entity Form
现在我对 Symfony 中实体表单类型的查询构建器功能有疑问。
此板上的其他问题没有帮助我找到解决方案:
参见例如:8456298 or 13846970 or enter link description here
情况如下:我想添加一个电子邮件地址以根据要求发送评分。 Ratings 是 Doctor class 的子级,而 Doctor class 是 User 的子级(此处使用 FOS User Bundle)
所以这是我的代码:
控制器:
public function AddPatientAction()
{
$user = $this->getUser();
$form = $this->createForm(new AddPremiumRatingType(), '', array('user'=> $user));
return $this->render('Acme/DemoBundle:Dashboard/Premium:addpatient.html.twig', array(
'form' => $form
));
}
这是 AddPremiumRatingType Class:
class AddPremiumRatingType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('doctor', 'entity', array(
'label' => 'Arztprofil',
'required' => true,
'class' => 'JBauleRatingBundle:Doctor',
'mapped' => false,
'property' => 'name',
'query_builder' => function(DoctorRepository $er) use ($options) {
$user = $options['user'];
return $er->createQueryBuilder('d')
->select('d')
->where('d.user = ?1')
->setParameter('1', $user->getId());
}))
->add('emails', 'collection', array(
'label' => 'E-Mail Adressen',
'type' => 'email',
'mapped' => false,
'delete_empty' => true,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__name__',
'options' => array(
'label' => 'E-Mail Adresse',
'attr' => array('class' => 'form-control'),
'required' => false
)
))
->add('save', 'submit', array(
'label' => 'Patienten hinzufügen'
));
}
public function getName()
{
return 'rating';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\PremiumRating',
));
$resolver->setRequired(array(
'user',
));
$resolver->setAllowedTypes(array(
'user' => 'Acme\LoginChildBundle\Entity\User',
));
}
}
最后这是我得到的错误:
Expected argument of type "object, array or empty", "string" given
我也试过:
外包存储库中的 QueryBuild Class 并调用存储库 class => 得到相同的错误
只使用创建查询命令 => 查询必须是 QueryBuilder 的实例的错误
正在阅读 Doctrine 和 Symfony 的文档...还没有找到解决方案
实际上,您根本无法呈现表单,因为您在创建时收到该错误。
$form = $this->createForm(new AddPremiumRatingType(), '', array('user'=> $user));
createForm()
接收的第二个参数被标记为 mixed
。正如错误所说,它必须是 object
或 array()
或者只是 null
.
将您的线路更改为:
$form = $this->createForm(new AddPremiumRatingType(), array(), array('user'=> $user));
现在我对 Symfony 中实体表单类型的查询构建器功能有疑问。
此板上的其他问题没有帮助我找到解决方案:
参见例如:8456298 or 13846970 or enter link description here
情况如下:我想添加一个电子邮件地址以根据要求发送评分。 Ratings 是 Doctor class 的子级,而 Doctor class 是 User 的子级(此处使用 FOS User Bundle)
所以这是我的代码:
控制器:
public function AddPatientAction()
{
$user = $this->getUser();
$form = $this->createForm(new AddPremiumRatingType(), '', array('user'=> $user));
return $this->render('Acme/DemoBundle:Dashboard/Premium:addpatient.html.twig', array(
'form' => $form
));
}
这是 AddPremiumRatingType Class:
class AddPremiumRatingType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('doctor', 'entity', array(
'label' => 'Arztprofil',
'required' => true,
'class' => 'JBauleRatingBundle:Doctor',
'mapped' => false,
'property' => 'name',
'query_builder' => function(DoctorRepository $er) use ($options) {
$user = $options['user'];
return $er->createQueryBuilder('d')
->select('d')
->where('d.user = ?1')
->setParameter('1', $user->getId());
}))
->add('emails', 'collection', array(
'label' => 'E-Mail Adressen',
'type' => 'email',
'mapped' => false,
'delete_empty' => true,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__name__',
'options' => array(
'label' => 'E-Mail Adresse',
'attr' => array('class' => 'form-control'),
'required' => false
)
))
->add('save', 'submit', array(
'label' => 'Patienten hinzufügen'
));
}
public function getName()
{
return 'rating';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\PremiumRating',
));
$resolver->setRequired(array(
'user',
));
$resolver->setAllowedTypes(array(
'user' => 'Acme\LoginChildBundle\Entity\User',
));
}
}
最后这是我得到的错误:
Expected argument of type "object, array or empty", "string" given
我也试过:
外包存储库中的 QueryBuild Class 并调用存储库 class => 得到相同的错误
只使用创建查询命令 => 查询必须是 QueryBuilder 的实例的错误
正在阅读 Doctrine 和 Symfony 的文档...还没有找到解决方案
实际上,您根本无法呈现表单,因为您在创建时收到该错误。
$form = $this->createForm(new AddPremiumRatingType(), '', array('user'=> $user));
createForm()
接收的第二个参数被标记为 mixed
。正如错误所说,它必须是 object
或 array()
或者只是 null
.
将您的线路更改为:
$form = $this->createForm(new AddPremiumRatingType(), array(), array('user'=> $user));