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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题