FOSRest产生通知:数组到字符串转换

时间:2016-03-20 11:58:06

标签: arrays json symfony fosrestbundle

我的FOSRestBUndle API中的测试和问题之间存在多对多的关系。如果我想通过帖子发布我正在创建的测试多个问题,我应该如何格式化我的json对象进行发布。我现在收到一条通知:数组到字符串转换。问题在我的FOSRestController

中设置为array = true

JSON

 {
    "event":"1",
    "testId":"3",
    "module":"1",
    "title":"Test"
    "description":"Test",
    "enabled":1,
    "isSpeedTest":1,
    "question":[1,2]
 }

TestQuestionsType.php

    <?php

namespace TeamGraduate\APIBundle\Form;

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

class TestQuestionsType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('body')
            ->add('allowableTime')
            ->add('created',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
            ->add('updated',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
            ->add('enabled')
            ->add('marks')
            ->add('topic')
            ->add('creatorUser')
            ->add('test')
            ->add('tag')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TeamGraduate\APIBundle\Entity\TestQuestions'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'teamgraduate_apibundle_testquestions';
    }
}

TestsType.php

<?php

namespace TeamGraduate\APIBundle\Form;

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

class TestsType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('description')
            ->add('created',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
            ->add('updated',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
            ->add('enabled')
            ->add('isSpeedTest')
            ->add('creatorUser')
            ->add('event')
            ->add('module')
            ->add('view')
            ->add('question', 'entity', array(
                'multiple' => true,
                'expanded' => false,
                'property' => 'name',
                'class' => 'TeamGraduate\APIBundle\Entity\TestQuestions'
            ))
            ->add('reportCard')
            ->add('cap')
            ->add('tag')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TeamGraduate\APIBundle\Entity\Tests'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'teamgraduate_apibundle_tests';
    }
}

如何发布多个问题以将测试作为json对象关联?

1 个答案:

答案 0 :(得分:1)

当测试发布时,我使用迭代遍历收到的问题数组:

/**
     * Processes the form.
     *
     * @param mixed $entity
     * @param array $parameters
     * @param String $method
     *
     * @return mixed
     *
     * @throws \APIBundle\Exception\InvalidFormException
     */
    private function processForm($entity, array $parameters, $method = "PUT") {

        $form = $this->formFactory->create(new TestsType(), $entity, array('method' => $method));

        foreach($parameters as $key=>$value)
        {
            if(is_null($value) || (is_array($value) && empty($value))) // remove null keys and empty arrays
                unset($parameters[$key]);
        }

        $form->submit($parameters, false || 'PATCH' !== $method);
        if ($form->isValid()) {
            $entity = $form->getData();

            foreach ($form['question']->getData()->getValues() as $v) {
                $question = $this->om->getRepository('APIBundle:TestQuestions')->find($v->getQuestionId());
                if ($question) {
                    $question->addTest($entity);
                }
            }

//            dump($entity);
//            die();
            $this->om->persist($entity);
            $this->om->flush($entity);
            return $entity;
        }
        throw new InvalidFormException('Invalid submitted data', $form);
    }
相关问题