symfony Notice: Array to string conversion choicetype multi dimensional

时间:2017-08-13 14:01:51

标签: arrays symfony multidimensional-array

I have a problem with choicetype on symfony 2.8 I tried to do this

I change my event entity like this

    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 result is : My array like sample

the code in my form is

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

but each time I have an error : Notice Array to string conversion

it seems the problem come from the method : getDisciplineLevels

when I do this in the form the result is correct but i need to change the name it's ok with level but not with 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

my entity event

<?php

namespace TR\CalendarBundle\Entity;

/** * Event */ class Event {

const ORANGE = '#ff7043';
const RED = '#ff1744';
const PINK = '#E6007E';

// Generated Code

/**
 * @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;
}

}

Thanks for your help. Fabrice

1 个答案:

答案 0 :(得分:0)

在控制器上调用从将原始数组转换为多维数组的实体的方法

**** CONTROLLER ****

$masterclass->setDisciplineLevels($disciplines_teacher_current);

**** ENTITY ****

在实体中使用getter和setter添加私有属性,并使用特殊方法__toString将表单中的每个结果转换为字符串,最后两个方法是在表单中使用并显示select

/*
* @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的对象调用multidimensionnal数组

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