【问题标题】:FOSRest Produces a Notice: Array to string conversionFOSRest 产生一个通知:数组到字符串的转换
【发布时间】:2016-07-06 22:04:21
【问题描述】:

我的 FOSRestBUndle API 中的测试和问题之间存在多对多关系。如果我想发布/关联多个问题与我通过帖子创建的测试,我应该如何格式化我的 json 对象以进行发布。我目前收到通知:数组到字符串的转换。 Question 在我的 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 对象与测试相关联?

【问题讨论】:

  • 您找到答案了吗?我也有同样的问题。
  • @LexHartman,看看我的回答。

标签: arrays json symfony fosrestbundle


【解决方案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);
    }

【讨论】:

  • 谢谢!现在我也会以这种方式实现它。
猜你喜欢
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多