symfony Notice: Array to string conversion choicetype multi dimensional

Fabrice

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

Fabrice

On the controller call the method from the entity who convert the original array to multidimensional array

**** CONTROLLER ****

$masterclass->setDisciplineLevels($disciplines_teacher_current);

**** ENTITY ****

In the entity add to private attributes with getters and setters and a special method __toString to convert each result from the form to string the two last methods is for using in the form and show the 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;
}

` **** FORM ****

You need to get the class array you set before in the controller

$eventinit = $builder->getData();

And after you just need to create your choiceType element with the the name of the attribute use for the method __toString and call the multidimensionnal array with the getter's object

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

choice field symfony "Notice: Array to string conversion "

From Dev

choice field symfony "Notice: Array to string conversion "

From Dev

notice: array to string conversion in php

From Dev

Notice: Array to string conversion PHP

From Dev

Notice Message: Array to string conversion

From Dev

JSON TO CSV Conversion Notice: Array to string conversion in

From Dev

Notice: Array to String conversion - But it's a string in an array

From Dev

Symfony fill ChoiceType with an array

From Dev

PHP Notice: Array to string conversion Error

From Dev

Notice: Array to string conversion when using Foreach

From Dev

Array to string conversion notice when using implode

From Dev

PHP Notice: Array to string conversion on line 10

From Dev

Notice Array to string conversion using nusoap

From Dev

PHP Notice: Array to string conversion on line 10

From Dev

Notice: Array to string conversion with update statment

From Dev

Notice: Array to string conversion $text = $htmlTag[0]

From Dev

FOSRest Produces a Notice: Array to string conversion

From Dev

Codeigniter: Severity notice Message Array to String conversion

From Dev

Autofill multi dimensional String[][] array

From Dev

modify array multi-dimensional symfony

From Dev

Multidimensional array from query into string gives Array to string conversion notice

From Dev

PHP: curl_setopt_array gives notice "array to string conversion"

From Dev

Notice: Array to string conversion. without array in code

From Dev

php convert a string into multi dimensional array

From Dev

String into a multi-dimensional array in Ruby?

From Dev

convert a string to multi dimensional array in php

From Dev

Getting string from multi-dimensional array

From Dev

PHP Flatten Multi Dimensional Array into String

From Java

How to solve PHP error 'Notice: Array to string conversion in...'

Related Related

  1. 1

    choice field symfony "Notice: Array to string conversion "

  2. 2

    choice field symfony "Notice: Array to string conversion "

  3. 3

    notice: array to string conversion in php

  4. 4

    Notice: Array to string conversion PHP

  5. 5

    Notice Message: Array to string conversion

  6. 6

    JSON TO CSV Conversion Notice: Array to string conversion in

  7. 7

    Notice: Array to String conversion - But it's a string in an array

  8. 8

    Symfony fill ChoiceType with an array

  9. 9

    PHP Notice: Array to string conversion Error

  10. 10

    Notice: Array to string conversion when using Foreach

  11. 11

    Array to string conversion notice when using implode

  12. 12

    PHP Notice: Array to string conversion on line 10

  13. 13

    Notice Array to string conversion using nusoap

  14. 14

    PHP Notice: Array to string conversion on line 10

  15. 15

    Notice: Array to string conversion with update statment

  16. 16

    Notice: Array to string conversion $text = $htmlTag[0]

  17. 17

    FOSRest Produces a Notice: Array to string conversion

  18. 18

    Codeigniter: Severity notice Message Array to String conversion

  19. 19

    Autofill multi dimensional String[][] array

  20. 20

    modify array multi-dimensional symfony

  21. 21

    Multidimensional array from query into string gives Array to string conversion notice

  22. 22

    PHP: curl_setopt_array gives notice "array to string conversion"

  23. 23

    Notice: Array to string conversion. without array in code

  24. 24

    php convert a string into multi dimensional array

  25. 25

    String into a multi-dimensional array in Ruby?

  26. 26

    convert a string to multi dimensional array in php

  27. 27

    Getting string from multi-dimensional array

  28. 28

    PHP Flatten Multi Dimensional Array into String

  29. 29

    How to solve PHP error 'Notice: Array to string conversion in...'

HotTag

Archive