我在 symfony 2.8 上的choicetype 有问题我试图这样做
我像这样更改我的事件实体
public function setDisciplineLevels($disciplines_to_order) {
$levelsbydiscipline = array();
foreach($disciplines_to_order as $disciplines) {
if(!array_key_exists($disciplines['discipline'], $levelsbydiscipline)){
$levelsbydiscipline[$disciplines['discipline']] = array();
}
$levelsbydiscipline[$disciplines['discipline']][$disciplines['teacherdiscipline_id'].$disciplines['level_id']]= $disciplines['title'];
}
$this->disciplinelevels= $levelsbydiscipline;
return $this;
}
我的数组结果是:我的数组像示例
我表单中的代码是
->add('disciplinelevels', ChoiceType::class, array(
'choices_as_values' => true,
'multiple' => true,
//'expanded' => true
));
但每次出现错误时:注意数组到字符串的转换
似乎问题来自方法:getDisciplineLevels
当我以表格形式执行此操作时,结果是正确的,但我需要更改名称,它可以用于 level 但不能用于 subjectlevels :
->add('level', ChoiceType::class, array(
'choices'=>array('informatique' => array(
'123'=>'Debutant',
'345'=>'Confirme',
'678'=>'Expert'
)),
'choices_as_values' => false,
'multiple' => false,
'expanded' => false
));
我的实体事件
<?php
命名空间 TR\CalendarBundle\Entity;
/** * 事件 */ 类事件 {
const ORANGE = '#ff7043';
const RED = '#ff1744';
const PINK = '#E6007E';
// 生成的代码
/**
* @var integer
*/
private $id;
/**
* @var \DateTime
*/
private $start;
/**
* @var \DateTime
*/
private $end;
/**
* @var string
*/
private $color;
/**
* @var integer
*/
private $status;
/**
* @var integer
*/
private $id_availability;
/**
* @var integer
*/
private $participants_max;
/**
* @var \TR\VisioBundle\Entity\Meeting
*/
private $meeting;
/**
* @var \TR\MainBundle\Entity\School
*/
private $school;
/**
* @var \TR\MainBundle\Entity\Teacher
*/
private $teacher;
/**
* @var \TR\MainBundle\Entity\Discipline
*/
private $discipline;
/**
* @var \TR\MainBundle\Entity\Level
*/
private $level;
/**
* @var array
*/
private $disciplinelevels;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set start
*
* @param \DateTime $start
*
* @return Event
*/
public function setStart($start)
{
$this->start = $start;
return $this;
}
/**
* Get start
*
* @return \DateTime
*/
public function getStart()
{
return $this->start;
}
/**
* Set end
*
* @param \DateTime $end
*
* @return Event
*/
public function setEnd($end)
{
$this->end = $end;
return $this;
}
/**
* Get end
*
* @return \DateTime
*/
public function getEnd()
{
return $this->end;
}
/**
* Set color
*
* @param string $color
*
* @return Event
*/
public function setColor($color)
{
$this->color = $color;
return $this;
}
/**
* Get color
*
* @return string
*/
public function getColor()
{
return $this->color;
}
/**
* Set status
*
* @param integer $status
*
* @return Event
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set idAvailability
*
* @param integer $idAvailability
*
* @return Event
*/
public function setIdAvailability($idAvailability)
{
$this->id_availability = $idAvailability;
return $this;
}
/**
* Get idAvailability
*
* @return integer
*/
public function getIdAvailability()
{
return $this->id_availability;
}
/**
* Set meeting
*
* @param \TR\VisioBundle\Entity\Meeting $meeting
*
* @return Event
*/
public function setMeeting(\TR\VisioBundle\Entity\Meeting $meeting = null)
{
$this->meeting = $meeting;
return $this;
}
/**
* Set Participants_max
*
* @param \TR\VisioBundle\Entity\Meeting $participants_max
*
* @return Event
*/
function setParticipantsMax($participants_max) {
$this->participants_max = $participants_max;
return $this;
}
/**
* Get meeting
*
* @return \TR\VisioBundle\Entity\Meeting
*/
public function getMeeting()
{
return $this->meeting;
}
/**
* Get Participants_max
*
* @return integer
*
*/
function getParticipantsMax() {
return $this->participants_max;
}
/**
* Set school
*
* @param \TR\MainBundle\Entity\School $school
*
* @return Event
*/
public function setSchool(\TR\MainBundle\Entity\School $school = null)
{
$this->school = $school;
return $this;
}
/**
* Get school
*
* @return \TR\MainBundle\Entity\School
*/
public function getSchool()
{
return $this->school;
}
/**
* Set teacher
*
* @param \TR\MainBundle\Entity\Teacher $teacher
*
* @return Event
*/
public function setTeacher(\TR\MainBundle\Entity\Teacher $teacher = null)
{
$this->teacher = $teacher;
return $this;
}
/**
* Get teacher
*
* @return \TR\MainBundle\Entity\Teacher
*/
public function getTeacher()
{
return $this->teacher;
}
/**
* Set discipline
*
* @param \TR\MainBundle\Entity\Discipline $discipline
*
* @return Event
*/
public function setDiscipline(\TR\MainBundle\Entity\Discipline $discipline = null)
{
$this->discipline = $discipline;
return $this;
}
/**
* Get discipline
*
* @return \TR\MainBundle\Entity\Discipline
*/
public function getDiscipline()
{
return $this->discipline;
}
/**
* Set level
*
* @param \TR\MainBundle\Entity\Level $level
*
* @return Event
*/
public function setLevel(\TR\MainBundle\Entity\Level $level = null)
{
$this->level = $level;
return $this;
}
/**
* Get level
*
* @return \TR\MainBundle\Entity\Level
*/
public function getLevel()
{
return $this->level;
}
/**
* Get levels by discipline
*
* @return \TR\MainBundle\Entity\Event
*/
public function setDisciplineLevels($disciplines_to_order) {
$levelsbydiscipline = array();
foreach($disciplines_to_order as $disciplines) {
if(!array_key_exists($disciplines['discipline'], $levelsbydiscipline)){
$levelsbydiscipline[$disciplines['discipline']] = array();
}
$levelsbydiscipline[$disciplines['discipline']][$disciplines['teacherdiscipline_id'].$disciplines['level_id']]= $disciplines['title'];
}
$this->disciplinelevels= $levelsbydiscipline;
return $this;
}
/**
* Get levels by discipline
*
* @return array
*/
public function getDisciplineLevels() {
return $this->disciplinelevels;
}
}
谢谢你的帮助。法布里斯
在控制器上调用将原始数组转换为多维数组的实体的方法
**** 控制器 ****
$masterclass->setDisciplineLevels($disciplines_teacher_current);
**** 实体 ****
在实体中添加具有 getter 和 setter 的私有属性以及一个特殊方法 __toString 将每个结果从表单转换为字符串,最后两个方法用于在表单中使用并显示选择
/*
* @var array
*/
private $disciplinelevels;
/*
* @var string
*/
private $listdisciplinelevels;
/*
* @var string
*/
public function __toString() {
return $this->listdisciplinelevels;
}
/* GETTERS AND SETTERS */
/*
* Get levels by discipline convert initial arrayt to multidimensionnal
*
* @return \TR\MainBundle\Entity\Event
*/
public function setDisciplineLevels($disciplines_to_order = array()) {
$levelsbydiscipline = array();
foreach($disciplines_to_order as $disciplines) {
if(!array_key_exists($disciplines['discipline'], $levelsbydiscipline)){
$levelsbydiscipline[$disciplines['discipline']] = array();
}
$levelsbydiscipline[$disciplines['discipline']][$disciplines['teacherdiscipline_id'].$disciplines['level_id']]= $disciplines['title'];
}
$this->disciplinelevels= $levelsbydiscipline;
return $this;
}
/**
* Get levels by discipline
*
* @return array
*/
public function getDisciplineLevels() {
return $this->disciplinelevels;
}
function getListdisciplinelevels() {
return $this->listdisciplinelevels;
}
function setListdisciplinelevels($listdisciplinelevels) {
$this->listdisciplinelevels = $listdisciplinelevels;
}
` **** 表格 ****
您需要在控制器中获取您之前设置的类数组
$eventinit = $builder->getData();
在您只需要创建带有用于方法 __toString 的属性名称的 choiceType 元素并使用 getter 的对象调用多维数组之后
->add('ListDisciplineLevels', ChoiceType::class, array(
'placeholder'=>'Do a choice',
'choices' => $choice = $eventinit->getDisciplineLevels(),
'choices_as_values' => false,
));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句