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

时间:2014-09-28 20:02:04

标签: php symfony

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

请您帮助我,并提前感谢您

最后一次错误显示:

  

捕获致命错误:传递给Project \ RmBundle \ Entity \ Posts :: setTags()的参数1必须是Project \ RmBundle \ Entity \ Tags的实例,给定字符串,在C:\ wamp \ www \ Test \中调用第438行的vendor \ symfony \ symfony \ src \ Symfony \ Component \ PropertyAccess \ PropertyAccessor.php,在C:\ wamp \ www \ Test \ src \ Project \ RmBundle \ Entity \ Posts.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个FormType PostsType.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())); }

1 个答案:

答案 0 :(得分:0)

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'); 
}

...