2017-08-13 66 views
0

我與symfony的2.8 choicetype一個問題,我試圖做到這一點symfony的注意:數組字符串轉換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; 
} 

我的陣列結果: My array like sample

在我的表單代碼

->add('disciplinelevels', ChoiceType::class, array(
       'choices_as_values' => true, 
       'multiple' => true, 
       //'expanded' => true 
      )); 

但每次我有一個錯誤:請注意數組字符串轉換

看來問題來自於方法: getDisciplineLevels

我做到這一點的形式,結果是正確的,當,但我需要更改名稱 它的確定與水平,但不與disciplinelevels:

->add('level', ChoiceType::class, array(
       'choices'=>array('informatique' => array(
        '123'=>'Debutant', 
        '345'=>'Confirme', 
        '678'=>'Expert' 
       )), 
       'choices_as_values' => false, 
       'multiple' => false, 
       'expanded' => false 
      )); 

stack trace

我實體事件

<?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; 
} 

}

感謝您的幫助。 法布里斯

+0

當你提交你的表格或在表單出現之前你有問題嗎? ,並且如果可能,你可以顯示爲你的班級 – ahmedbhs

+0

你的示例數組中的選擇值(提交給後端的數據)和選擇標籤(顯示爲選項的名稱)應該是什麼?應該按照第一級別鍵(屏幕截圖中的「Informatique」)對值進行分組? – nifr

+0

問題是在渲染表單女巫類之前你需要實體嗎?是的'Informatique'是第一級 - >和第二級是

回答

0

在控制器上的呼叫從實體誰原始數組轉換爲多維數組

**** ****控制器

$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; 
} 

` **** **** FORM

你需要獲取類數組您在控制器

$eventinit = $builder->getData();

之前設置

然後,您只需要使用方法__toString使用的屬性名稱創建choiceType元素,然後使用getter的對象調用多維數組

->add('ListDisciplineLevels', ChoiceType::class, array(
    'placeholder'=>'Do a choice', 
    'choices' => $choice = $eventinit->getDisciplineLevels(), 
    'choices_as_values' => false, 
));