2014-09-28 56 views
2

我有一個Symfony2的問題,我想做一個標籤系統,但我無法設法做我嘗試了很多,但有很多錯誤。Symfony2和輸入文本的標籤系統

請你能幫助我,謝謝你提前

的最後一個錯誤顯示:

Catchable Fatal Error: Argument 1 passed to Project\RmBundle\Entity\Posts::setTags() must be an instance of Project\RmBundle\Entity\Tags, string given, called in C:\wamp\www\Test\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 438 and defined in C:\wamp\www\Test\src\Project\RmBundle\Entity\Posts.php line 390

Posts.php

​​

,我有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())); } 

回答

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

... 
+0

thnx但不顯示我的表單上的文本字段 – roki 2014-09-28 20:19:52