在symfony上使用BeelabTagBundle库

时间:2018-08-13 16:57:30

标签: symfony tags

我正在尝试使用BeelabTagBundle库在我的网站上实现标签。但是我缺乏这样做的信息。这是我现在的位置:

  • 通过作曲家安装
  • 创建了一个实现“ TaggableInterface”的实体,其中包含一些交友字段,以及文档中要求的字段。

 /**
  * Constructor
  */
 public function __construct()
 {
     $this->keywords = new ArrayCollection();
 }
 /**
  * @ORM\Id
  * @ORM\Column(type="integer")
  * @ORM\GeneratedValue(strategy="AUTO")
  */
 protected $id = null;

 /**
  * @ORM\Column(type="string", length=255)
  */
 private $type;

 /**
  * @ORM\Column(type="string", length=255)
  */
 private $fileName;

 /**
  * @ORM\Column(type="boolean")
  */
 private $active;

 /**
  * @var ArrayCollection
  *
  * @ORM\ManyToMany(targetEntity="Tag")
  */
 private $keywords;

 private $tagsText;

 /**
  * @ORM\Column(type="datetime", nullable=true)
  */
 private $updated;

 /**
  * {@inheritdoc}
  */
 public function addTag(TagInterface $tag)
 {
     $this->keywords[] = $tag;
 }

 /**
  * {@inheritdoc}
  */
 public function removeTag(TagInterface $tag)
 {
     $this->keywords->removeElement($tag);
 }

 /**
  * {@inheritdoc}
  */
 public function hasTag(TagInterface $tag)
 {
     return $this->keywords->contains($tag);
 }

 /**
  * {@inheritdoc}
  */
 public function getTags()
 {
     return $this->keywords;
 }

 /**
  * {@inheritdoc}
  */
 public function getTagNames(): array
 {
     return empty($this->tagsText) ? [] : array_map('trim', explode(',', $this->tagsText));
 }

 /**
  * @param string
  */
 public function setTagsText($tagsText)
 {
     $this->tagsText = $tagsText;
     $this->updated = new \DateTime();
 }

 /**
  * @return string
  */
 public function getTagsText()
 {
     $this->tagsText = implode(', ', $this->keywords->toArray());
     return $this->tagsText;
 }
  • 拥有获取我的标签json的路线(调用时可以工作,但为空)

 /**
  * @Route("/miseenpage/keywords.json", name="keywords", defaults={"_format": "json"})
  */
 public function tagsAction() {
     $tags = $this->getDoctrine()->getRepository(Tag::class)->findBy([], ['name' => 'ASC']);
     return $this->render('miseenpage/keywords.html.twig', ['tags' => $tags]);
 }

 class TagsTextType extends AbstractType
 {

     public function buildForm(FormBuilderInterface $builder, array $options)
     {
         $builder
             ->add('tagsText', TextType::class, ['required' => false, 'label' => 'Tags'])
         ;
     }

     /**
      * @var RouterInterface $route
      */
     private $router;

     /**
      * @param RouterInterface $router
      */
     public function __construct(RouterInterface $router)
     {
         $this->router = $router;
     }

     /**
      * {@inheritdoc}
      */
     public function configureOptions(OptionsResolver $resolver)
     {
         $resolver->setDefaults([
             'required' => false,
             'label' => 'Tags',
             'attr' => [
                 'placeholder' => 'separate tags with comma',
                 'data-ajax' => $this->router->generate('keywords'),
             ],
         ]);
     }

     /**
      * {@inheritdoc}
      */
     public function getParent()
     {
         return TextType::class;
     }
 }

我现在如何显示我的标签字段(以及我实体中的所有其他数据)?文档中未对此进行详细说明。谢谢

0 个答案:

没有答案