Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

forms - Symfony2 - array to string conversion error

I've read the other subjects but it doesn't solve my problem so:

I've got this

->add('role', 'choice', array(
                'label' => 'I am:',
                'mapped' => true,
                'expanded' => true,
                'multiple' => false,
                'choices' => array(
                    'ROLE_NORMAL' => 'Standard',
                    'ROLE_VIP' => 'VIP',
                ) 
            ))

And whatever I do, I get this error:

Notice: Array to string conversion in C:xampphtdocsxxxvendorsymfonysymfony  srcSymfonyComponentFormExtensionCoreChoiceListChoiceList.php line 458 

In my form type the setRole method is not even called (when I rename it to some garbage the error still occurs). Why is this happening?

// EDIT

Full stack trace:

in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormExtensionCoreChoiceListChoiceList.php at line 458  -

     */
    protected function fixIndex($index)
    {
        if (is_bool($index) || (string) (int) $index === (string) $index) {
            return (int) $index;
        }

    at ErrorHandler ->handle ('8', 'Array to string conversion', 'C:xampphtdocs     xxxvendorsymfonysymfonysrcSymfonyComponentFormExtensionCoreChoiceListChoiceList.php', '458', array('index' => array()))
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormExtensionCoreChoiceListChoiceList.php at line 458  +
at ChoiceList ->fixIndex (array())
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormExtensionCoreChoiceListChoiceList.php at line 476  +
at ChoiceList ->fixIndices (array(array()))
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormExtensionCoreChoiceListSimpleChoiceList.php at line 152  +
at SimpleChoiceList ->fixChoices (array(array()))
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormExtensionCoreChoiceListChoiceList.php at line 204  +
at ChoiceList ->getIndicesForChoices (array(array()))
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormExtensionCoreDataTransformerChoiceToBooleanArrayTransformer.php at line 63  +
at ChoiceToBooleanArrayTransformer ->transform (array())
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormForm.php at line 1019  +
at Form ->normToView (array())
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormForm.php at line 332  +
at Form ->setData (array())
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormExtensionCoreDataMapperPropertyPathMapper.php at line 59  +
at PropertyPathMapper ->mapDataToForms (object(User), object(RecursiveIteratorIterator))
in C:xampphtdocsxxxvendorsymfonysymfonysrcSymfonyComponentFormForm.php at line 375  +
at Form ->setData (object(User))
in C:xampphtdocsxxxvendorfriendsofsymfonyuser-bundleFOSUserBundleControllerRegistrationController.php at line 49  +
at RegistrationController ->registerAction (object(Request))
at call_user_func_array (array(object(RegistrationController), 'registerAction'), array(object(Request)))
in C:xampphtdocsxxxappootstrap.php.cache at line 2770  +
at HttpKernel ->handleRaw (object(Request), '1')
in C:xampphtdocsxxxappootstrap.php.cache at line 2744  +
at HttpKernel ->handle (object(Request), '1', true)
in C:xampphtdocsxxxappootstrap.php.cache at line 2874  +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
in C:xampphtdocsxxxappootstrap.php.cache at line 2175  +
at Kernel ->handle (object(Request))
in C:xampphtdocsxxxwebapp_dev.php at line 29  +
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Symfony's trying to convert your $role(array property) to not multiple choice field(string).

There's several ways to deal with this problem:

  1. Set multiple to true in your choice form widget.
  2. Change mapping from array to string for $role property in your entity.
  3. If you insist to have above options unchanged, you can create DataTransformer. That's not the best solution because you will lose data if your array have more than 1 element.

Example:

<?php
namespace AcmeDemoBundleFormDataTransformer;

use SymfonyComponentFormDataTransformerInterface;
use SymfonyComponentFormExceptionTransformationFailedException;

class StringToArrayTransformer implements DataTransformerInterface
{
    /**
     * Transforms an array to a string. 
     * POSSIBLE LOSS OF DATA
     *
     * @return string
     */
    public function transform($array)
    {
        return $array[0];
    }

    /**
     * Transforms a string to an array.
     *
     * @param  string $string
     *
     * @return array
     */
    public function reverseTransform($string)
    {
        return array($string);
    }
}

And then in your form class:

use AcmeDemoBundleFormDataTransformerStringToArrayTransformer;
/* ... */
$transformer = new StringToArrayTransformer();
$builder->add($builder->create('role', 'choice', array(
                'label' => 'I am:',
                'mapped' => true,
                'expanded' => true,
                'multiple' => false,
                'choices' => array(
                    'ROLE_NORMAL' => 'Standard',
                    'ROLE_VIP' => 'VIP',
                )
              ))->addModelTransformer($transformer));

You can read more about DataTransformers here: http://symfony.com/doc/current/cookbook/form/data_transformers.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...