注意:数组到字符串转换symfony2

时间:2016-04-28 09:24:32

标签: symfony symfony-forms fosrestbundle

我为这个错误读了几个解决方案,但它没有用,我不确切知道问题出在哪里,因为其他获取请求有效 我的formType如下:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('image', null, array('property_path' => 'file'))
        ->add('tags', null, array('mapped' => false))
        ->add('machinetags', null, array('mapped' => false));
}

控制器的功能如下:

      /**
 * @ApiDoc(description="Associate photo with tags.")
 *
 * @ParamConverter("photo", class="TestTaskPhotosBundle:Photo")
 *
 * @Rest\Post("/photos/{id}/tags")
 * @Rest\RequestParam(name="tags", requirements=".+", nullable=false, map=true, description="Tags that associates photo.")
 * @Rest\View()
 */
 public function postTagsToPhotoAction(Photo $photo, array $tags)
{
    $em = $this->getDoctrine()->getManager();

    //TODO: add validation and maybe form

    if ($tags) {
        $tags = $em->getRepository('TestTaskTagsBundle:Tag')->findOrCreateByTitles($tags);
    }

    foreach ($tags as $tag) {
        $photo->addTag($tag);
    }

    $em->persist($photo);
    $em->flush();

    return array('photo' => $photo);
}

1 个答案:

答案 0 :(得分:1)

解决了,问题出在cotroller功能,解决方案如下:

        public function postMachinetagsToPhotoAction($id, array $machinetags)
{
    $em = $this->getDoctrine()->getManager();

    //TODO: add validation and maybe form
   $photo = $em->getRepository('TestTaskPhotosBundle:Photo')->find($id);
    if ($machinetags) {
        $machinetags = $em->getRepository('TestTaskMachineTagsBundle:MachineTag')->findOrCreateByTitles($machinetags);
    }

    foreach ($machinetags as $machinetag) {
        $photo->addMachineTag($machinetag);
    }

    $em->persist($photo);
    $em->flush();

    return array('photo' => $photo);
}