skywalker14
skywalker14

Reputation: 33

Array to string conversion ArrayChoiceList

I have read every thread regarding this error and I can't figure out where this is coming from.

I have a form with a lot of checkbox fields that are being populated from arrays. However in my Entity I am converting all the fields to a comma separated string (yes I know this is ugly) and the fields in question all work fine, however I'm getting this error and I simply can't figure out where the issue is coming from. When I was debugging these fields it gave me the arrays that were in question and I was able to fix them

I also have another 4 set of EntityType choice lists that populate from 4 other database tables. All data pulls and works properly for all the Entities but it's when I'm trying to persist the database that I get this error, but I can't find any reference to what Entity or array/field this is referring to. All I'm seeing below are empty arrays.

If seeing my Entity and FormType helps I can post those.

[1] Symfony\Component\Debug\Exception\ContextErrorException: Notice: Array to string conversion
at n/a
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php line 73

at Symfony\Component\Debug\ErrorHandler->handleError('8', 'Array to string conversion', '/Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php', '73', array('choice' => array('')))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php line 73

at Symfony\Component\Form\ChoiceList\ArrayChoiceList->Symfony\Component\Form\ChoiceList\{closure}(array(''))
    in  line 

at call_user_func(object(Closure), array(''))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php line 158

at Symfony\Component\Form\ChoiceList\ArrayChoiceList->getValuesForChoices(array(array('')))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php line 37

at Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer->transform(array(''))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 1092

at Symfony\Component\Form\Form->normToView(array(''))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 352

at Symfony\Component\Form\Form->setData(array(''))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 57

at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(Programs), object(RecursiveIteratorIterator))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 385

at Symfony\Component\Form\Form->setData(object(Programs))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 477

at Symfony\Component\Form\Form->initialize()
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 226

at Symfony\Component\Form\FormBuilder->getForm()
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 39

at Symfony\Component\Form\FormFactory->create('AppBundle\Form\ProgramsType', object(Programs), array())
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 309

at Symfony\Bundle\FrameworkBundle\Controller\Controller->createForm('AppBundle\Form\ProgramsType', object(Programs))
    in /Applications/MAMP/htdocs/mpdb/src/AppBundle/Controller/DefaultController.php line 37

at AppBundle\Controller\DefaultController->nominateAction(object(Request))
    in  line 

at call_user_func_array(array(object(DefaultController), 'nominateAction'), array(object(Request)))
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 148

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 66

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
    in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 169

at Symfony\Component\HttpKernel\Kernel->handle(object(Request))

INFO - Matched route "{route}". 
INFO - Populated the TokenStorage with an anonymous Token. 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - SELECT k0_.title AS title_0, k0_.slug AS slug_1, k0_.id AS id_2 FROM keywords k0_ 
DEBUG - SELECT c0_.title AS title_0, c0_.slug AS slug_1, c0_.is_effstrat AS is_effstrat_2, c0_.id AS id_3 FROM categories c0_ WHERE c0_.is_effstrat = 'YES' 
DEBUG - SELECT r0_.title AS title_0, r0_.category AS category_1, r0_.report_text AS report_text_2, r0_.id AS id_3 FROM risk_factors r0_
DEBUG - SELECT p0_.title AS title_0, p0_.category AS category_1, p0_.report_text AS report_text_2, p0_.id AS id_3 FROM protective_factors p0_ 
CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Notice: Array to string conversion" at /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php line 73 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 
DEBUG - Notified event "{event}" to listener "{listener}". 

MY database structure has 5 main tables and joining tables for each manytomany

programs (main table)
- id
- other columns

keywords
- id
- other columns

programs_keywords
- program_id
- keyword_id

and so on for tables categories, protective_factors, risk_factors

Programs Entity

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Categories", inversedBy="program")
 * @ORM\JoinTable(name="programs_categories",
 *   joinColumns={
 *     @ORM\JoinColumn(name="program_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 *   }
 * )
 * @Assert\Count(min = 1, minMessage = "You must select at least one option.")
 */
private $category;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Keywords", inversedBy="program")
 * @ORM\JoinTable(name="programs_keywords",
 *   joinColumns={
 *     @ORM\JoinColumn(name="program_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="keyword_id", referencedColumnName="id")
 *   }
 * )
 * @Assert\Count(min = 1, minMessage = "You must select at least one option.")
 */
private $keyword;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="ProtectiveFactors", inversedBy="program")
 * @ORM\JoinTable(name="programs_protective_factors",
 *   joinColumns={
 *     @ORM\JoinColumn(name="program_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="protective_factor_id", referencedColumnName="id")
 *   }
 * )
 * @Assert\Count(min = 1, minMessage = "You must select at least one option.")
 */
private $protectiveFactor;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="RiskFactors", inversedBy="program")
 * @ORM\JoinTable(name="programs_risk_factors",
 *   joinColumns={
 *     @ORM\JoinColumn(name="program_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="risk_factor_id", referencedColumnName="id")
 *   }
 * )
 * @Assert\Count(min = 1, minMessage = "You must select at least one option.")
 */
private $riskFactor;

ProgramsType form class This has my entities added, but also as mentioned has a ton of other ChoiceType fields that are populated from arrays, but these all seem to work fine.

        ->add('keyword', EntityType::class, array( 'class' => 'AppBundle:Keywords', 'choice_label' => 'title', 'multiple' => true, 'expanded' => true ))
        ->add('riskFactor', EntityType::class, array( 'class' => 'AppBundle:RiskFactors', 'choice_label' => 'title', 'multiple' => true, 'expanded' => true ))
        ->add('protectiveFactor', EntityType::class, array( 'class' => 'AppBundle:ProtectiveFactors', 'choice_label' => 'title', 'multiple' => true, 'expanded' => true ))
        ->add('category', EntityType::class, array( 'class' => 'AppBundle:Categories', 'query_builder' => function (EntityRepository $er) {
            $qb = $er->createQueryBuilder('c');
            $qb->where($qb->expr()->eq('c.isEffstrat', $qb->expr()->literal('YES')));
            return $qb;
        },'choice_label' => 'title', 'multiple' => true, 'expanded' => true ))

Upvotes: 0

Views: 2347

Answers (1)

skywalker14
skywalker14

Reputation: 33

Finally solved this. The issue was that I was converting ChoiceType fields that were not set to multiple = true, to an array and back in my Entity.

Simple mistake but overlooked it so many times.

Upvotes: 2

Related Questions