带有Symfony2和输入文本的标签系统


a system of tags with Symfony2 and input text

我对Symfony2有问题,我想制作一个标签系统,但我做不到。我尝试了很多,但有很多错误。

请你帮我,提前谢谢

最后一个错误显示:

可捕获的致命错误:传递给Project''RmBundle''Entity''Posts:setTags()的参数1必须是Project''RmPundle''Entity''Tags的实例,给定字符串,在第438行的C:''wamp''www''Test''vendor''symfony''symfony ''src''symfony''Component''PropertyAccess''PropertyAAccessor.php中调用,并在C:''wamp''www''Test''src''Project''RmBoundle''Entity''Pposts.php第390行中定义

Posts.php

class Posts{
/**
 * @var integer
 *
 * @ORM'Column(name="id", type="integer")
 * @ORM'Id
 * @ORM'GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var ArrayCollection $tags
 *
 * @ORM'ManyToMany(targetEntity="Project'RmBundle'Entity'Tags", inversedBy="posts")
 */
 private $tags;

/**
 * Constructor
 */
public function __construct()
{
    $this->tags = new 'Doctrine'Common'Collections'ArrayCollection();
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
/**
 * Add tags
 *
 * @param 'Portfolio'GeneralBundle'Entity'Tags $tags
 * @return Article
 */
public function addTag('Project'RmBundle'Entity'Tags $tags)
{
    $this->tags[] = $tags;
    return $this;
}
/**
 * Remove tags
 *
 * @param 'Project'RmBundle'Entity'Tags $tags
 */
public function removeTag('Project'RmBundle'Entity'Tags $tags)
{
    $this->tags->removeElement($tags);
}
/**
 * Get tags
 *
 * @return 'Doctrine'Common'Collections'Collection 
 */
public function getTags()
{
    return $this->tags;
}

/**
 * Set tags
 *
 * @param 'Project'RmBundle'Entity'Tags $tags
 * @return Tags
 */
public function setTags('Project'RmBundle'Entity'Tags $tags)
{
    $this->tags[] = $tags;
    return $this;
}}

并且我有2个FormTypePostsType.php

class PostsType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('tags', 'text');     
}}

TagsType.php

class TagsType extends AbstractType{
     private $om;
public function __construct(ObjectManager $om)
{
    $this->om = $om;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $transformer = new StringToTagsTransformer($this->om);
    $builder->addModelTransformer($transformer);
}
 /**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Project'RmBundle'Entity'Tags'
    ));
} 
public function getName()
{
    return 'tags';}}

我为创建了一个转换

class StringToTagsTransformer implements DataTransformerInterface{
private $om;
public function __construct(ObjectManager $om)
{
    $this->om = $om;
}
public function reverseTransform($ftags)
{
    $tags = new ArrayCollection();
    $tag = strtok($ftags, ",");
    while($tag !== false) {
        $itag = new Tags();
        $itag->setName($tag);
        if(!$tags->contains($itag))
            $tags[] = $itag;
        $tag = strtok(",");
    }
    return $tags;
}
public function transform($tags)
{
    $ftags = "";
    if($tags != null) {
    foreach($tags as $tag)
        $ftags = $ftags.','.$tag->getName();
    }
    return $ftags;}}

最后是我的控制器PostsController.php

   public function addBlogAction(Request $request){
      $em = $this->getDoctrine()->getManager();
      $posts = new Posts();
      $form = $this->createForm(new PostsType, $posts, array(
        'action' => $this->generateUrl('project_add_post'),
        'method' => 'POST'
        ));
     $em->getRepository('ProjectRmBundle:Tags')->filter($posts->getTags());
      if('POST' == $request->getMethod()){
        $form->handleRequest($request);
           if ($form->isValid()) {
              foreach($posts->getTags() as $tag){
                  $em->persist($tag);
              }
           }
            $em->persist($posts);
            $em->flush();
            return $this->redirect($this->generateUrl('project_posts'));
          }
  }
      return $this->render('ProjectRmBundle:Posts:add.html.twig', array('form'=>$form->createView())); }

PostsType中,您已将tags字段定义为具有类型text。你想让它使用你的TagsType,所以你应该这样做:

class PostsType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('tags', 'tags');     
}}

这是假设您已经将其声明为服务。

更新:

您需要将text字段放在TagsType中。你可以这样做:

class TagsType extends AbstractType{
...
public function buildForm(FormBuilderInterface $builder, array $options)
{
    ...
    $builder
        ->add('name', 'text'); 
}
...