【问题标题】:How can I store data from entity type selectbox (Symfony)?如何存储来自实体类型选择框(Symfony)的数据?
【发布时间】:2018-12-13 11:04:06
【问题描述】:

我有一个使用 formbuilder 创建的选择框:

<select id="form_type" name="form[type]" class="form-control select2 select2-hidden-accessible" tabindex="-1" aria-hidden="true">
  <option value="1">text</option>
  <option value="2">hidden</option>
  <option value="3">password</option>
  <option value="4" selected="selected">icon</option>
  <option value="5">select</option>
</select>

这是在我的控制器中创建它的方式:

$formBuilder->add('type', EntityType::class, array(
            'attr' => array('class' => 'form-control select2'), 'label' => 'Type',
            'class' => FieldTypes::class,
            'choice_label' => function ($fieldTypes) {
              return $fieldTypes->getName();
              }
            ));

$formBuilder->add('cancel', ButtonType::class, array('label' => 'cancel','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar')))
        ->add('save', SubmitType::class, array('label' => 'Save','attr' => array('id' => 'submit-my-beautiful-form','class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')));
        $form = $formBuilder->getForm();

        $form->handleRequest($request);

但是当我想保存选择时,我收到错误消息:

传递给 App\Entity\Fields::setType() 的参数 1 必须是一个实例 App\Entity\FieldTypes 或 null,给定字符串,调用 /Users/work/project/src/Controller/PagesController.php 第 242 行

在我的实体中:

 public function setType(?FieldTypes $type): self
  {
    $this->type = $type;
    return $this;
  }

它是这样存储的:

$entity = $this->getDoctrine()->getRepository($EntityName)->find($data['form[id]']);
$em = $this->getDoctrine()->getManager();
foreach ($fields as $field) {
   $em = $this->getDoctrine()->getManager();
   $func = 'set'.$field['fieldName'];
   $args = $data['form['.$field['fieldName'].']'];
   $entity->$func($args);
}
$em->flush();

【问题讨论】:

    标签: symfony controller doctrine entity formbuilder


    【解决方案1】:

    使用方法$form-&gt;handleRequest($request)。它将使用其配置填充您的表单(它将使用发布的 id 实例化 FieldType)。

    在这里,您手动添加选择的“原始”值,它是包含FieldType id 的字符串。

    编辑:见docs

    【讨论】:

    • 我已经有了这个 `$form->handleRequest($request);` 只是没有在这里发布,一定是另一个问题(我会更新我的问题
    • 我假设您使用了一个表单类并使用$this-&gt;createForm(Form::class, $type) 方法创建它,抱歉。在这种情况下,$type 变量会自动填充表单配置后的表单发布值。这是使用表单的最正确方法,您应该看看这个;)
    【解决方案2】:

    根据您的代码,它有点难以理解,但是 如果 $field 是一个字符串。

    foreach ($fields as $field) {
    $obj = $this->getDoctrine()->getRepository($fieldEntity)->find(field);      
    $entiy->setType($obj);
    }
    

    【讨论】:

    • 谢谢!我不确定,field 是什么,是相关的列名吗?这就是实体的关联方式:/** * @ORM\ManyToOne(targetEntity="FieldTypes") * @ORM\JoinColumn(name="type", referencedColumnName="id") */ private $type;
    • 它像这样测试你的代码:$fieldEntity = new FieldTypes(); $obj = $this-&gt;getDoctrine()-&gt;getRepository($fieldEntity)-&gt;find(id); $entiy-&gt;setType($obj);
    • 现在的错误是:Warning: Illegal offset type in isset or empty
    【解决方案3】:

    在表单提交块之后尝试

    $entity->setType($form->get('type'));
    

    $form->get('type') 将作为 FieldTypes 类返回。

    如果你使用 $field 而不是 $field['fieldName'], 将对象返回。 例如,

    $func = 'set'.$field;
    

    【讨论】:

    • $func = 'set'.$field; 给了我错误Notice: Array to string conversion
    猜你喜欢
    • 2023-03-25
    • 2022-01-20
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    相关资源
    最近更新 更多