hdevs/tag-bundle

Symfony HDevsTagBundle

安装: 17

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

类型:symfony-bundle

1.0.1 2017-11-02 18:02 UTC

This package is not auto-updated.

Last update: 2024-09-21 02:00:24 UTC


README

步骤 1:下载包

打开命令行,进入你的项目目录,然后执行以下命令以下载此包的最新稳定版本

$ composer require hdevs/tag-bundle

此命令要求你全局安装了Composer,如Composer文档中的安装章节所述。

步骤 2:启用包

然后,将包添加到项目app/AppKernel.php文件中已注册的包列表中,以启用该包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new HDevs\TagBundle\HDevsTagBundle(),
        );

        // ...
    }

    // ...
}

步骤 3:示例

在app/config/config.yml中

h_devs_tag:
    entity_class: AppBundle\Entity\Tag

标签实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use HDevs\TagBundle\Model\Tag as BaseTag

/**
 * Tag
 *
 * @ORM\Table(name="tag")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\TagRepository")
 */
class Tag extends BaseTag
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}

帖子实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use HDevs\TagBundle\Behavior\Taggable;

/**
 * Event
 *
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
 */
class Post implements Taggable
{
    // other fields

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Tag")
     */
    private $tags;

    /**
     * @var string
     */
    private $tagsText;


    public function __construct(){
        $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add tag
     *
     * @param \HDevs\TagBundle\Model\Tag $tag
     *
     * @return Event
     */
    public function addTag(\HDevs\TagBundle\Model\Tag $tag)
    {
        $this->tags[] = $tag;

        return $this;
    }

    /**
     * Remove tag
     *
     * @param \HDevs\TagBundle\Model\Tag $tag
     */
    public function removeTag(\HDevs\TagBundle\Model\Tag $tag)
    {
        $this->tags->removeElement($tag);
    }

    /**
     * Get tags
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTags()
    {
        return $this->tags;
    }

    /**
     * @return string
     */
    public function getTagsText()
    {
        return $this->tagsText;
    }

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


}

步骤 4:添加行为

src/AppBundle/Behavior/TagBehavior

namespace AppBundle\Behavior;


use HDevs\TagBundle\Model\Tag;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use HDevs\TagBundle\Behavior\TagBehavior as TagBehaviorInterface

class TagBehavior implements TagBehaviorInterface
{

    /**
     * @var TokenStorage
     */
    private $tokenStorage;

    public function __construct(TokenStorage $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function run(Tag $tag)
    {
        $tag->setUser($this->tokenStorage->getToken()->getUser());
    }

    public function validate(Tag $tag)
    {
        if( $this->tokenStorage->getToken()->getUser() != $tag->getUser() ){
            $t = new \AppBundle\Entity\Tag();
            $t->setUser($this->tokenStorage->getToken()->getUser());
            $t->setValue($tag->getValue());
            return $t;
        }
        return $tag;
    }
}

在app/config/services.yml中

services:
    #other services
    
    app.behavior.tag:
        class: AppBundle\Behavior\TagBehavior
        arguments: ["@security.token_storage"]
        public: true
    

在app/config/config.yml中

h_devs_tag:
    entity_class: AppBundle\Entity\Tag
    behavior_class: app.behavior.tag