Symfony2 - 数组到字符串的转换错误 [英] Symfony2 - array to string conversion error
问题描述
我读过其他主题,但并没有解决我的问题:
我有这个
- > add('role','choice',array(
'label'=>'我是:',
'
'expanded'=> true,
'multiple'=> false,
'choices'=>数组(
'ROLE_NORMAL' =>'Standard',
'ROLE_VIP'=>'VIP',
)
))
无论我做什么,我都会遇到这个错误:
注意:Array to string转换在C:\xampp\htdocs\xxx\vendor\symfony\symfony \src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php 458行
在我的表单类型中,setRole方法甚至不是calle d(当我将它重命名为垃圾时,错误仍然存在)。为什么会发生这种情况?
//编辑
完整堆栈跟踪:
在C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\\ \\ Extension \Core\ChoiceList\ChoiceList.php在458行 -
* /
保护函数fixIndex($ index)
{
if(is_bool ($ index)||(string)(int)$ index ===(string)$ index){
return(int)$ index;
}
在ErrorHandler - > handle('8','Array to string conversion','C:\xampp\htdocs \xxx\vendor\symfony\\ \\'symfony \ src \Symfony \Component\Form\Extension\Core\ChoiceList\ChoiceList.php','458',array('index'=> array()))
在C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php在行在ChoiceList中458 +
- > fixIndex(array())
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\第476行的Component \Form\Extension\Core\ChoiceList\ChoiceList.php + ChoiceList中的
→> fixIndices(array(array()))
in C:\xampp \htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\\第152行的ChoiceList \ SimpleChoiceList.php + SimpleChoiceList中的
- > fixChoices(array(array()))
in C:\xampp\htdocs\xxx\vendor\symfony \symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php在204行+
在ChoiceList - > getIndicesForChoices(array(array()) )
在C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToBooleanArrayTransformer第63行的.php + ChoiceToBooleanArrayTransformer中的
- > transform(array())
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\\ \\ Symfony \ Component \Form\Form.php在第1019行+
在Form - > normToView(array())
在C:\xampp\htdocs\xxx\ vendor\symfony\symfony\src\Symfony\Component\Form\\第332行的Form.php + Form中的
- > setData(array())
在C:\xampp\htdocs\xxx\vendor\symfony\symfony\src \Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper.php在59行+
在PropertyPathMapper - > mapDataToForms(object(User),object(RecursiveIteratorIterator))
在C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php在375行+
在Form - > setData(object(User))
in C:\xampp\htdocs\xxx\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Controller\RegistrationController。 php在49行+
在RegistrationController - > registerAction(object(Request))
在call_user_func_array(array(object(RegistrationController),'registerAction'),array(object(Request)))$ b $ C在C:\xampp\htdocs\xxx\app\bootstrap.p在2770行的hp.cache +在HttpKernel的
- > handleRaw(object(Request),'1')
在C:\xampp\htdocs\xxx\app\bootstrap。在2744行的php.cache + HttpKernel的
- > handle(object(Request),'1',true)
in C:\xampp\htdocs\xxx\app\在2874行的bootstrap.php.cache + ContainerAwareHttpKernel的
- > handle(object(Request),'1',true)
在C:\xampp\htdocs\xxx\app \ bootstrap.php.cache在2175行+
在Kernel - > handle(object(Request))
在C:\xampp\htdocs\xxx\web\app_dev。 php在第29行+
Symfony试图将你的$角色(数组属性)不是多个选择字段(字符串)。
有几种方法可以解决这个问题:
- 将 多个 设置为 true 你的选择形式wid 从 array > $ role 属性。
- 如果您坚持要使上述选项保持不变,您可以创建DataTransformer。
这不是最好的解决方案,因为如果您的数组中有多个元素,那么将会丢失数据。
示例:
<?php
命名空间Acme \DemoBundle\\ \\Form\DataTransformer;
使用Symfony \ Component \ Form \ DataTransformerInterface;
使用Symfony \ Component \Form\Exception\TransformationFailedException;
class StringToArrayTransformer实现DataTransformerInterface
{
/ **
*将数组转换为字符串。
*可能的数据损失
*
* @return字符串
* /
公共函数转换($ array)
{
return $阵列[0];
}
/ **
*将字符串转换为数组。
*
* @param string $ string
*
* @return array
* /
public function reverseTransform($ string)
{
返回数组($ string);
$ / code>
然后在你的表单类中:
使用Acme\DemoBundle\Form\DataTransformer\StringToArrayTransformer;
/ * ... * /
$ transformer = new StringToArrayTransformer();
$ builder-> add($ builder-> create('role','choice',array(
'label'=>'我是:',
'映射'=> true,
'expanded'=> true,
'multiple'=> false,
'choices'=> array(
'ROLE_NORMAL'= >'Standard',
'ROLE_VIP'=>'VIP',
)
)) - > addModelTransformer($ transformer));
您可以在这里阅读有关DataTransformers的更多信息: http://symfony.com/doc/current/cookbook/form/data_transformers.html
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:\xampp\htdocs\xxx\vendor\symfony\symfony \src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.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:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.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:\xampp\htdocs \xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php', '458', array('index' => array()))
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php at line 458 +
at ChoiceList ->fixIndex (array())
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php at line 476 +
at ChoiceList ->fixIndices (array(array()))
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList.php at line 152 +
at SimpleChoiceList ->fixChoices (array(array()))
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php at line 204 +
at ChoiceList ->getIndicesForChoices (array(array()))
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToBooleanArrayTransformer.php at line 63 +
at ChoiceToBooleanArrayTransformer ->transform (array())
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 1019 +
at Form ->normToView (array())
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 332 +
at Form ->setData (array())
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper.php at line 59 +
at PropertyPathMapper ->mapDataToForms (object(User), object(RecursiveIteratorIterator))
in C:\xampp\htdocs\xxx\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 375 +
at Form ->setData (object(User))
in C:\xampp\htdocs\xxx\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Controller\RegistrationController.php at line 49 +
at RegistrationController ->registerAction (object(Request))
at call_user_func_array (array(object(RegistrationController), 'registerAction'), array(object(Request)))
in C:\xampp\htdocs\xxx\app\bootstrap.php.cache at line 2770 +
at HttpKernel ->handleRaw (object(Request), '1')
in C:\xampp\htdocs\xxx\app\bootstrap.php.cache at line 2744 +
at HttpKernel ->handle (object(Request), '1', true)
in C:\xampp\htdocs\xxx\app\bootstrap.php.cache at line 2874 +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
in C:\xampp\htdocs\xxx\app\bootstrap.php.cache at line 2175 +
at Kernel ->handle (object(Request))
in C:\xampp\htdocs\xxx\web\app_dev.php at line 29 +
Symfony's trying to convert your $role(array property) to not multiple choice field(string).
There's several ways to deal with this problem:
- Set multiple to true in your choice form widget.
- Change mapping from array to string for $role property in your entity.
- 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 Acme\DemoBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
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 Acme\DemoBundle\Form\DataTransformer\StringToArrayTransformer;
/* ... */
$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
这篇关于Symfony2 - 数组到字符串的转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!