Forms Symfony2-数组到字符串转换错误
我读过其他科目,但它不能解决我的问题,因此: 我有这个Forms Symfony2-数组到字符串转换错误,forms,symfony,roles,Forms,Symfony,Roles,我读过其他科目,但它不能解决我的问题,因此: 我有这个 ->add('role', 'choice', array( 'label' => 'I am:', 'mapped' => true, 'expanded' => true, 'multiple' => false, 'choices' => a
->add('role', 'choice', array(
'label' => 'I am:',
'mapped' => true,
'expanded' => true,
'multiple' => false,
'choices' => array(
'ROLE_NORMAL' => 'Standard',
'ROLE_VIP' => 'VIP',
)
))
无论我做什么,我都会犯这样的错误:
Notice: Array to string conversion in C:\xampp\htdocs\xxx\vendor\symfony\symfony \src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php line 458
在我的表单类型中,甚至没有调用setRole方法(当我将其重命名为某个垃圾时,仍然会发生错误)。为什么会这样
//编辑
完整堆栈跟踪:
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正在尝试将$role(数组属性)转换为not多选字段(字符串) 有几种方法可以解决此问题:
<?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);
}
}
您可以在此处阅读有关DataTransformers的更多信息:确保在ORM文件中使用正确的数据类型。在这种情况下,角色字段不能为字符串。它必须是多对多关系、数组或json_数组 如果选择其中一个,symfony将毫不费力地插入数据或任何类型的转换器 例如: 因此,它将以如下方式存在于您的数据库中:
a:2:{i:0;s:4:"user";i:1;s:5:"admin";}
我只添加了一个DataTransformer,而不更改我的角色属性的数组类型,然后将其放入我的UserType中:
use AppBundle\Form\DataTransformer\StringToArrayTransformer;
//...
$transformer = new StringToArrayTransformer();
$builder->get('roles')->addModelTransformer($transformer);
这对我很有效。我有你的问题。。我用这个解决方案解决了这个问题。我希望它能帮助你 此代码在登录和注册表单上工作 用户实体
class User {
/**
* @ORM\Column(type="array")
*/
private $roles ;
public function getRoles()
{
$roles = $this->roles;
var_dump($roles);
if ($roles != NULL) {
return explode(" ",$roles);
}else {
return $this->roles;
}
}
public function setRoles($roles)
{
$this->roles = $roles;
}
用户类型
->add('roles', ChoiceType::class, array(
'attr' => array(
'class' => 'form-control',
'value' => $options[0]['roles'],
'required' => false,
),
'multiple' => true,
'expanded' => true, // render check-boxes
'choices' => [
'admin' => 'ROLE_ADMIN',
'user' => 'ROLE_USER',
]
))
请提供完整的堆栈跟踪-否则没有人可以看到错误的位置它的真实来源完整的堆栈跟踪已提供通过它发生的方式只有当我将mapped to true(现在是)mapped true设置为默认值时-您可以编辑它。如果不想将值添加到表单的基础实体,则只需将其设置为false。如果使用“映射”会发生什么情况?当我获得足够的声誉时,我会这样做,因为可能值得注意的是,此答案特别适用于数组。如果表单引用的是一个对象(例如,我的例子中的用户对象),那么转换器需要引用用户对象。例如,在transformer中,我在“transform”函数中使用了$user->getRoles()[0],您的解决方案是正确的。然而,这毫无意义。如果您的解决方案基于丢失数据,则不可接受@user2394156您只需要对ORM文件进行一点点更改。确保您的字段是数组或json_数组。如此,Symfony2将把您的数据作为序列化数据或json数据插入数据库列。
class User {
/**
* @ORM\Column(type="array")
*/
private $roles ;
public function getRoles()
{
$roles = $this->roles;
var_dump($roles);
if ($roles != NULL) {
return explode(" ",$roles);
}else {
return $this->roles;
}
}
public function setRoles($roles)
{
$this->roles = $roles;
}
->add('roles', ChoiceType::class, array(
'attr' => array(
'class' => 'form-control',
'value' => $options[0]['roles'],
'required' => false,
),
'multiple' => true,
'expanded' => true, // render check-boxes
'choices' => [
'admin' => 'ROLE_ADMIN',
'user' => 'ROLE_USER',
]
))