dongyong6045 2013-06-22 16:12
浏览 54
已采纳

Symfony2 - 使用ArrayCollection填充文本字段?

I am using the FPNTagBundle and I would like to have a text field to add tags to entities that works in the same way as the one on this site.

I can create a new entity with tags no problem by using explode but when I come to edit the entity again, I get something like this in the text field.

Doctrine\Common\Collections\ArrayCollection@0000000062a07bb50000000047044868

Is there a way I can pre populate a text field with the array collection, so that all of the tags appear, separated by a space?

Here's what I currently have in my controller:

public function editpageAction(Request $request, $id = NULL)
{
    $em = $this->getDoctrine()->getEntityManager();
    $tagManager = $this->get('fpn_tag.tag_manager');
    $page = new Page();

    if ( ! empty($id))
    {
        $page = $em->getRepository('ContentBundle:Page')->findOneById($id);
        $tagManager->loadTagging($page);
    }

    $form = $this->createForm(new PageType(), $page);

    if ($request->isMethod('POST')) 
    {
        $form->bind($request);

        if ($form->isValid()) 
        {   
            $em = $this->getDoctrine()->getManager();
            $em->persist($page);
            $em->flush();

            return $this->redirect($this->generateUrl('content_admin_list_sections'));
        }
    }

    return $this->render('ContentBundle:Admin:page.html.twig', array('form' => $form->createView()));
}

Any advice appreciated.

Thanks

  • 写回答

1条回答 默认 最新

  • douwen5833 2013-06-22 16:58
    关注

    This is what the Data transfomers are made for.

    How to use Data Transformers

    A simple example:

    public function transform($tags)
    {
        $tags = $tags->toArray();
    
        if (count($tags) < 1)
            return '';
        else
            return implode(' ', $tags);
    }
    
    public function reverseTransform($string)
    {
        $tags = new ArrayCollection();
        $tagsArray = explode(' ', $string);
    
        if (count($tagsArray) > 0)
            $tags = new ArrayCollection($tagsArray);
    
        return $tags;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?