【发布时间】:2017-01-09 04:50:07
【问题描述】:
我想将一个对象从控制器传递到我的表单构建器,以便稍后将其用于我的 ChoiceType 字段。我该如何做到这一点?
这是我的控制器:
$choices = [];
$table2Repository = $this->getDoctrine()->getRepository('SwipeBundle:Company');
$table2Objects = $table2Repository->findAll();
foreach ($table2Objects as $table2Obj) {
$choices[$table2Obj->getId()] = $table2Obj->getId() . ' - ' . $table2Obj->getName();
}
$form = $this->createForm(SubAgentType::class, $choices, array(
'action'=>$this->generateUrl('backend_sub_agent_create'),
'method'=>'POST'
));
这是我的 SubAgentType.php
class SubAgentType extends AbstractType {
protected $choices;
public function __construct (Choices $choices)
{
$this->choices = $choices;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('company_id', ChoiceType::class, array(
'mapped' => false,
'choices' => $choices,
));
问题是我收到以下错误。
可捕获的致命错误:参数 1 传递给 MyBundle\Form\SubAgentType::__construct() 必须是 MyBundle\Form\Choices,
【问题讨论】: