Stalin Kay Stalin Kay - 4 months ago 25
JSON Question

FOSRest Produces a Notice: Array to string conversion

I have a many-to-many relationship between tests and questions in my FOSRestBUndle API. How am I supposed to format my json object for posting if I want to post/associate multiple questions with the test that I'm creating through a post.I'm currently getting a Notice: Array to string conversion. Question is set to array=true in my FOSRestController

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';
}
}


How can I post multiple questions to associate with a test as a json object?

Answer

I resorted to iterating through the received question array when a test is posted as follows:

/**
     * 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);
    }