简体   繁体   English

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

[英]Symfony3.0.3 ChoiceType choose among entity entries via checkboxes and save the chosen to simple_array MySQL

Here is the situation: 情况如下:

  1. I have this Entity jobtypes that contains name and description fields 我有包含namedescription字段的实体工作jobtypes
  2. I am making registration form and the user that is registering is supposed to be able to choose among the jobtypes entries by their name. 我正在制作注册表,正在注册的用户应该能够通过其名称在jobtypes条目中进行选择。 So far so good 到现在为止还挺好
  3. I want to list the available options ( jobtypes entries) by checkboxes, so the user can choose one or more an then save the checked ones to a simple_array type in MySQL. 我想通过复选框列出可用的选项( jobtypes条目),以便用户可以选择一个或多个,然后将选中的选项保存到MySQL中的simple_array类型。
  4. I am building the form using Symfony 3.0.3 我正在使用Symfony 3.0.3构建表单

The problem: 问题:

Everything looks good, until i check the database. 一切看起来不错,直到我检查数据库。 The chosen jobtypes are not displayed by the names but with some weird kind of path? 所选的作业类型不是按名称显示,而是带有某种奇怪的路径?

Example: 例:

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

I get Doctrine\\Common\\Collections\\ArrayCollection@0000000010bec20a0000000010aaccb8 我得到 Doctrine \\ Common \\ Collections \\ ArrayCollection @ 0000000010bec20a0000000010aaccb8

The question 问题

How should i build my ->add() field in Symfony in order to get this done? 我应该如何在Symfony中构建我的-> add()字段以完成此操作?

This is what i have so far 这就是我到目前为止

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

this is how i get the data 这就是我获取数据的方式

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

and how i set it to an object 以及我如何将其设置为对象

$sprovider->setInterestedin($interestedin);

sprovider.php sprovider.php

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

I can't think of anything else to show you that might help you answer my question. 我想不出其他任何可以帮助您回答我问题的方法。 If there is something i missed, please tell. 如果我错过了什么,请告诉。

EDIT 编辑

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

i get 我得到

array(1) { [0]=> object(Doctrine\\Common\\Collections\\ArrayCollection)#489 (1) { ["elements":"Doctrine\\Common\\Collections\\ArrayCollection":private]=> array(2) { [0]=> object(AppBundle\\Entity\\jobtypes)#482 (3) { ["id":"AppBundle\\Entity\\jobtypes":private]=> int(1) ["name":"AppBundle\\Entity\\jobtypes":private]=> string(15) "Kitchen Fitting" ["description":"AppBundle\\Entity\\jobtypes":private]=> string(32) "Building a kitchen from scratch." } [1]=> object(AppBundle\\Entity\\jobtypes)#487 (3) { ["id":"AppBundle\\Entity\\jobtypes":private]=> int(2) ["name":"AppBundle\\Entity\\jobtypes":private]=> string(16) "Bathroom Fitting" ["description":"AppBundle\\Entity\\jobtypes":private]=> string(24) "Rennovate your bathroom." } } } }

I think i'm not getting only the names of the jobs in $interestedin , but the whole entries. 我认为我不仅获得$interestedin中的工作名称,还获得整个条目。 Hope that helps a bit. 希望有点帮助。

Why do you want to store them as an array? 为什么要将它们存储为数组? It looks like a ManyToMany relation . 看起来就像是ManyToMany关系

For simple_array type, Doctrine will store in a db field and imploded and serialized representation of objects array. 对于simple_array类型,Doctrine将存储在db字段中以及对象数组的内爆和序列化表示形式。 You'll lose basic filtering options on a mysql side. 您将失去mysql方面的基本过滤选项。

Here is the deal. 这是交易。 After crawling the internet for an answer I ended empty hands and decided to try everything. 在搜寻互联网的答案之后,我空手而归,决定尝试一切。

Here is the solution: 解决方法如下:

This is the form entry 这是表格条目

->add('interestedin', ChoiceType::class, array(
'expanded'=>true,'multiple'=>true,'choices'=>array_combine($interestedin,$interestedin)))

This is how I get the choices 这就是我得到选择的方式

$jobtypes = $this->getDoctrine()
        ->getRepository('AppBundle:jobtypes')
        ->findAll();

    $interestedin = array();
    foreach($jobtypes as $jt){
        array_push($interestedin,$jt->getName());
    }

Results: 结果:

Image 图片

Database shows the chosen checkboxes as strings instead of the weird path. 数据库将选择的复选框显示为字符串而不是奇怪的路径。 The first entry I left for comparison ( how it was before ). 我留下来进行比较的第一个条目(之前如何)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM