Symfony将数据设置为多选 [英] Symfony set data to multiple choiceType

查看:143
本文介绍了Symfony将数据设置为多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令从控制器内部设置symfony choiceType值:

I set symfony choiceType value from inside the controller by using this:

    $editForm->get('userJobTitle')->setData($job->getJobTitle()->getId());

对于多选类型该如何做?以下方法无效

How to do it for multiple choiceType? the following method isn't working

 $editForm->get('userskills')->setData($job->getSkills());

其中getSkills函数返回Doctrine集合.

where getSkills function return Doctrine collection.

推荐答案

setData()方法需要包含选定选项值的字符串数组,所以我要这样做:

setData() method requires array of strings which contain the values of selected options so i do:

$usSkills = $job->getSkills()->getValues();
        $vals = array();
        foreach ($usSkills as $us){
            $vals[] = (string)$us->getId();
        }
        $editForm->get('userskills')->setData($vals); 

这解决了问题

这篇关于Symfony将数据设置为多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆