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

时间:2014-08-14 12:12:07

标签: forms symfony

我正在尝试建立一个回答小型调查的表格。

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

  

表单的视图数据应该是标量,数组或类型   \ ArrayAccess的实例,但是是类的实例   VS \ MyProject的\ UserBundle \实体\用户。你可以避免这个错误   设置" data_class"选项   " VS \ myproject的\ UserBundle \实体\用户"或者通过添加视图转换器   转换类的实例   VS \ myproject \ UserBundle \ Entity \ User转换为标量,数组或实例   \ ArrayAccess接口。

我无法在第一时间看到我将使用用户实体的位置,并且很难从此错误消息中创建一些内容。有人可以帮忙吗?

其他信息

堆栈跟踪

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   + 

数据模型

([]中的实体

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

在构建表单时,为每个[SurveyItem]创建一个[SurveyItemResult],用于当前[CustomerSurvey]。

控制器

构建调查回复表单的控制器(基于实体[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()));

    }

}

表单类型

这是我为[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';
    }
}

这是应该嵌入的[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';
    }
}

1 个答案:

答案 0 :(得分:5)

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

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

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

    $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);

IdToObjectTransformer类https://gist.github.com/korotovsky/eeedb6e5d8f6bd9dca38

的示例

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

它在Symfony2中非常强大。