如何在Symfony(嵌入)表单中解决此数据类型冲突?

mat*_*jay 0 forms symfony

我正在尝试建立一个表格来回答一个小调查.

在使用我的表单加载页面时,我收到此错误:

表单的视图数据应该是标量,数组或\ ArrayAccess的实例,但是类VS\myproject\UserBundle\Entity\User的实例.您可以通过将"data_class"选项设置为"VS\myproject\UserBundle\Entity\User"或添加视图转换器来将类VS\myproject\UserBundle\Entity\User的实例转换为标量,数组或\ ArrayAccess的一个实例.

我无法在第一时间看到我将使用User实体的位置,并且很难从这个错误消息中做出一些事情.有人可以帮忙吗?

附加信息

堆栈跟踪

in <myprojectdirectorypath>\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 353   + 

at Form ->setData (object(User)) 
in <myprojectdirectorypath>\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper.php at line 57   + 

at PropertyPathMapper ->mapDataToForms (object(CustomerSurvey), object(RecursiveIteratorIterator)) 
in <myprojectdirectorypath>\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 385   + 

at Form ->setData (object(CustomerSurvey)) 
in <myprojectdirectorypath>\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 477   + 

at Form ->initialize () 
in <myprojectdirectorypath>\vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php at line 230   + 

at FormBuilder ->getForm () 
in <myprojectdirectorypath>\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 39   + 

at FormFactory ->create (object(CustomerSurveyType), object(CustomerSurvey), array()) 
in <myprojectdirectorypath>\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php at line 181  + 

at Controller ->createForm (object(CustomerSurveyType), object(CustomerSurvey)) 
in <myprojectdirectorypath>\src\VS\Myapp\MobileBundle\Controller\SurveyController.php at line 33   + 

at SurveyController ->takeSurveyAction ('1') 

at call_user_func_array (array(object(SurveyController), 'takeSurveyAction'), array('1')) 
in <myprojectdirectorypath>\app\bootstrap.php.cache at line 2969   + 

at HttpKernel ->handleRaw (object(Request), '1') 
in <myprojectdirectorypath>\app\bootstrap.php.cache at line 2931   + 

at HttpKernel ->handle (object(Request), '1', true) 
in <myprojectdirectorypath>\app\bootstrap.php.cache at line 3080   + 

at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in <myprojectdirectorypath>\app\bootstrap.php.cache at line 2330   + 

at Kernel ->handle (object(Request)) 
in <myprojectdirectorypath>\web\app_dev.php at line 28   + 
Run Code Online (Sandbox Code Playgroud)

数据模型

([]中的实体)

[Survey]具有一个或多个[SurveyItem],可以作为[CustomerSurvey]分配给一个或多个[User].

在构建表单时,为每个[SurveyItem],为当前[CustomerSurvey]创建[SurveyItemResult].

调节器

构建调查响应表单的控制器(基于实体[CustomerSurvey]并嵌入所有相关的[SurveyItemResult])如下所示:

<?php

namespace VS\Myapp\MobileBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use VS\Myapp\WebBundle\Entity\SurveyItemResult;
use VS\Myapp\MobileBundle\Form\Type\CustomerSurveyType;

class SurveyController extends Controller
{
    public function indexAction()
    {
        $customerSurveys = $this->getUser()->getCustomerSurveys();

        return $this->render('VSMyappMobileBundle:Survey:index.html.twig', array('customerSurveys' => $customerSurveys));
    }

    public function takeSurveyAction($customerSurveyId)
    {

        $customerSurvey = $this->getDoctrine()->getRepository('VSMyappWebBundle:CustomerSurvey')->find($customerSurveyId);

        foreach($customerSurvey->getSurvey()->getItems() as $surveyItem)
        {

            $csr = new SurveyItemResult();
            $csr->setSurveyItem($surveyItem);

            $customerSurvey->addResult($csr);
        }

        $form = $this->createForm(new CustomerSurveyType(), $customerSurvey);

        return $this->render('VSMyappMobileBundle:Survey:takeSurvey.html.twig', array( 'form' => $form->createView()));

    }

}
Run Code Online (Sandbox Code Playgroud)

表格类型

这是我为[CustomerSurvey]构建的表单类型:

<?php
namespace VS\Myapp\MobileBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CustomerSurveyType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('results', 'collection', array('type' => new SurveyItemResultType()));

        $builder->add('customer', 'hidden');
        $builder->add('survey', 'hidden');

        $builder->add('save', 'submit', array('label' => 'Submit your answer'));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VS\Myapp\WebBundle\Entity\CustomerSurvey',
        ));
    }

    public function getName()
    {
        return 'customerSurvey';
    }
}
Run Code Online (Sandbox Code Playgroud)

这是应该嵌入的[SurveyItemResult]的表单类型:

<?php
namespace VS\Myapp\MobileBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class SurveyItemResultType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('textResult', 'text');

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VS\Myapp\WebBundle\Entity\SurveyItemResult',
        ));
    }

    public function getName()
    {
        return 'surveyItemResult';
    }
}
Run Code Online (Sandbox Code Playgroud)

Dmi*_*kii 5

当您尝试将customer对象和survey对象映射到标量值(作为整数)时会发生这种情况:

    $builder->add('customer', 'hidden');
    $builder->add('survey', 'hidden');
Run Code Online (Sandbox Code Playgroud)

要避免此问题,请将其更改为:

    $user = $builder->create('customer', 'hidden');
    $user->addViewTransformer(new IdToObjectTransformer($entityManager, 'FQCN of User model'));

    $survey = $builder->create('survey', 'hidden');
    $survey->addViewTransformer(new IdToObjectTransformer($entityManager, 'FQCN of Survey model'));

    $builder->add($user);
    $builder->add($survey);
Run Code Online (Sandbox Code Playgroud)

IdToObjectTransformer类的示例https://gist.github.com/korotovsky/eeedb6e5d8f6bd9dca38

有关数据变形金刚的更多信息,请阅读:http://symfony.com/doc/current/cookbook/form/data_transformers.html#model-and-view-transformers

这在Symfony2中确实很有用.