damianociarla/tag-bundle

DCSTagBundle 为您的 Symfony 项目添加标签功能,可关联任意数量的不同实体

v1.1.1 2014-08-23 06:17 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:47:34 UTC


README

DCSTagBundle 为您的 Symfony 项目添加标签功能,可关联任意数量的不同实体。

安装

a) 下载并安装 DCSTagBundle

要安装 DCSTagBundle,请运行以下命令

bash $ php composer.phar require damianociarla/tag-bundle

b) 启用包

要启用它,请将包实例添加到内核中

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
    	// ...
    	new DCS\TagBundle\DCSTagBundle(),
	);
}

2) 创建您的标签类

在此第一个版本中,DCSTagBundle 仅支持 Doctrine ORM。但是,您必须提供一个具体的标签类。您必须扩展包提供的抽象实体,并创建适当的映射。

a) 注释

<?php
// src/Acme/TagBundle/Entity/Tag.php

namespace Acme\TagBundle\Entity;

use DCS\TagBundle\Entity\Tag as BaseTag;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="tag")
 */
class Tag extends BaseTag
{

}

b) xml

<?php
// src/Acme/TagBundle/Entity/Tag.php

namespace Acme\TagBundle\Entity;

use DCS\TagBundle\Entity\Tag as BaseTag;

class Tag extends BaseTag
{

}

XML 映射文件

<!-- src/Acme/TagBundle/Resources/config/doctrine/Tag.orm.xml -->

<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="Acme\TagBundle\Entity\Tag" table="tag__tags" />

</doctrine-mapping>

3) 配置您的应用程序

# app/config/config.yml

dcs_tag:
    db_driver: orm
    model: Acme\TagBundle\Entity\Tag

4) 将关系添加到您的实体类中

<?php
// src/Acme/BlogBundle/Entity/Post.php

namespace Acme\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="post")
 */
class Post
{
    //... stuff

    /**
     * @ORM\ManyToMany(targetEntity="Acme\TagBundle\Entity\Tag", cascade={"remove", "persist"})
     * @ORM\JoinTable(name="post_has_tag",
     *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
     *      )
     */
    protected $tags;

    function __construct()
    {
        //... stuff

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

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

        return $this;
    }

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

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

5) 使用 "dcs_tag" 表单类型

<?php
// src/Acme/BlogBundle/Form/Type/PostFormType.php

namespace Acme\BlogBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class PostFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            //... stuff
            ->add('tags', 'dcs_tag')
        ;
    }
}

6) 如何保存标签

如果您使用表单,不需要 Tag 模型管理器(dcs_tag.manager)来持久化实体,因为它已经由 DataTransformer 管理,但如果您想手动添加单个标签到集合中,可以使用以下代码

$tagManager = $this->container->get('dcs_tag.manager');

$post = new Post();
$post->addTag($tagManager->add('tag-to-add'));

//... persist the post object

add 方法插入一个新标签,如果已不存在,则返回找到的标签。