FOSRest 出一个 Notice: Array to string conversion
FOSRest Produces a Notice: Array to string conversion
我的 FOSRestBUndle API 中的测试和问题之间存在多对多关系。如果我想通过 post.I 创建的测试 post/associate 多个问题,我应该如何为 posting 格式化我的 json 对象a 注意:数组到字符串的转换。问题在我的 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';
}
}
如何 post 多个问题作为 json 对象与测试相关联?
当发布测试时,我求助于遍历接收到的问题数组:
/**
* 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);
}
我的 FOSRestBUndle API 中的测试和问题之间存在多对多关系。如果我想通过 post.I 创建的测试 post/associate 多个问题,我应该如何为 posting 格式化我的 json 对象a 注意:数组到字符串的转换。问题在我的 FOSRestController
中设置为 array=truejson
{
"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';
}
}
如何 post 多个问题作为 json 对象与测试相关联?
当发布测试时,我求助于遍历接收到的问题数组:
/**
* 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);
}