php - Symfony3.0.3 ChoiceType 通过复选框在实体条目中进行选择并将所选内容保存到 simple_array MySQL

标签 php mysql symfony

情况如下:

  1. 我有这个实体 jobtypes,其中包含 namedescription 字段
  2. 我正在制作注册表单,正在注册的用户应该能够按其姓名在 jobtypes 条目中进行选择。到目前为止一切顺利
  3. 我想通过复选框列出可用选项(jobtypes 条目),以便用户可以选择一个或多个,然后将选中的选项保存到 MySQL 中的 simple_array 类型。
  4. 我正在使用 Symfony 3.0.3 构建表单

问题:

一切看起来都很好,直到我检查数据库。所选的工作类型不是按名称显示的,而是用某种奇怪的路径显示的?

示例:

我想要'name1','name2',......

我明白 Doctrine\Common\Collections\ArrayCollection@0000000010bec20a0000000010aaccb8

问题

我应该如何在 Symfony 中构建我的 ->add() 字段才能完成此任务?

这就是我到目前为止所拥有的

->add('interestedin', CollectionType::class, array(
            'entry_type'=>ChoiceType::class, array(
                'expanded'=>true,'multiple'=>true,'choices'=>array(
                   'class'=>'AppBundle\Entity\jobtypes','choice_label'=>'name'))))

这就是我获取数据的方式

$interestedin = array();
$interestedin[] = $form['interestedin']->getData();

以及我如何将其设置为对象

$sprovider->setInterestedin($interestedin);

sprovider.php

/**
 * @var array
 *
 * @ORM\Column(name="interestedin", type="simple_array")
 */
private $interestedin;

我想不出其他任何东西可以向您展示可以帮助您回答我的问题。如果有什么遗漏的地方,请告诉我。

编辑

当我{{ dump(sprovider.interestedin) }}

我明白

array(1) { [0]=> object(Doctrine\Common\Collections\ArrayCollection)#489 (1) { ["elements":"Doctrine\Common\Collections\ArrayCollection":private]= > 数组(2) { [0]=> 对象(AppBundle\Entity\jobtypes)#482 (3) { ["id":"AppBundle\Entity\jobtypes":private]=> int(1) ["名称":"AppBundle\Entity\jobtypes":private]=> string(15) "厨房装修"["description":"AppBundle\Entity\jobtypes":private]=> string(32) "从头开始 build 厨房。"} [1]=> 对象(AppBundle\Entity\jobtypes)#487 (3) { ["id":"AppBundle\Entity\jobtypes":private]=> int(2) ["name":"AppBundle\Entity\jobtypes":private]=> string(16) "浴室装修"["description":"AppBundle\Entity\jobtypes":private]=> string(24) "翻新你的浴室。"} } } }

我认为我不仅获得了 $interestedin 中的职位名称,还获得了整个条目。希望能有所帮助。

最佳答案

为什么要将它们存储为数组? 它看起来像 ManyToMany relation .

对于 simple_array 类型,Doctrine 将存储在数据库字段中以及对象数组的内爆和序列化表示。您将失去 mysql 端的基本过滤选项。

关于php - Symfony3.0.3 ChoiceType 通过复选框在实体条目中进行选择并将所选内容保存到 simple_array MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36451514/

相关文章:

php - Python xml-rpc 与 PHP 客户端和 unicode 不工作

php - 上传文件到服务器

php - 在 PHP 中使用 secure 和 httponly 选项删除 cookie 值得吗?

由 c++ Mysql C API mysql_real_escape_string

php - bind_param 与其他字符连接

rest - 在 Symfony2 中 PUT 请求数据

php - 从数据库呈现的全日历事件不考虑时区

mysql - 如何将 Tomcat 配置为不使用数据库连接池

php - 路由顺序 symfony 4

php - Symfony:从缓存中禁用环境变量