icap-lyon1/simple-tag-bundle

Symfony TagBundle,可轻松为任何实体添加标签

安装: 599

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 5

分支: 2

开放问题: 1

类型:symfony-bundle

2.0.0 2013-03-01 08:08 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:37:50 UTC


README

Symfony2 扩展包,用于轻松管理任何实体的标签。

安装

要安装此扩展包,请按照以下步骤操作

首先在您的 composer.json 文件中添加依赖项

"require": {
    ...
    "icap-lyon1/simple-tag-bundle": "2.0.*"
},

然后使用以下命令安装扩展包

php composer update

在您的应用程序内核中启用扩展包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new ICAPLyon1\Bundle\SimpleTagBundle\ICAPLyon1SimpleTagBundle(),
    );
}

然后更新您的数据库模式

php app/console doctrine:schema:update --force

然后安装扩展包资源

php app/console assets:install

// if you want to create a symlink:

php app/console assets:install --symlink

最后,将扩展包配置文件包含到您的应用程序配置文件中

// app/config/config.yml
imports:
    // ...
    - { resource: @ICAPLyon1SimpleTagBundle/Resources/config/config.yml }

现在扩展包已安装。

如何使用

为了向实体添加标签,实体必须实现 TaggableInterface 接口示例

<?php
// Acme/Bundle/AcmeBundle/Entity/TaggableEntity.php

namespace Acme\Bundle\AcmeBundle\Entity;

use ICAPLyon1\Bundle\SimpleTagBundle\Entity\TaggableInterface;

class TaggableEntity implements TaggableInterface
{ 
    // Your code here
}

然后,当您希望将实体与标签关联时,只需调用 icaplyon1_simpletag.manager 服务以创建表单并按以下说明处理即可

// Instead of standard form creation
// $form = $this->createForm(new MyObjectType(), $myObject);

// Do this:
$form = $this->get('icaplyon1_simpletag.manager')->createForm(
    new TaggableEntityType(),
    $entity);

要保存并将标签与实体关联,请按如下方式调用 processForm 函数

if ($form->isValid()) {
    $myObject = $this->get('icaplyon1_simpletag.manager')->processForm($form);

    return $this->redirect($this->generateUrl(...));
}

processForm($form) 方法将检索输入标签,添加新的(尚未关联的)标签,并删除输入列表中未包含的已关联标签

icaplyon1_simpletag.manager 函数的功能

关联标签

如果您想将标签与实体关联

// ...
//Associate tags with your entity
$this->get("icaplyon1_simpletag.manager")->addTag($tag, $entity);
// ...

如果您想将多个标签与实体关联

// ...
//Associate tags with your entity
$this->get("icaplyon1_simpletag.manager")->addTags($tags, $entity);
// ...

解除标签关联

如果您想解除实体与标签的关联

$this->get("icaplyon1_simpletag.manager")->removeTag($tag, $entity);

如果您想解除实体与多个标签的关联

$this->get("icaplyon1_simpletag.manager")->removeTags($tags, $entity);

从一个实体中移除所有标签

如果您想从实体中移除所有标签(在删除实体时执行此操作,以避免在数据库中保留垃圾数据

$this->get("icaplyon1_simpletag.manager")->removeAllTags($entity);

方法示例

// ...
public function deleteAction(Request $request, $id)
{
    $form = $this->createDeleteForm($id);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('ICAPLyon1TestTagBundle:TaggableEntity')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find TaggableEntity entity.');
        }
        
        //Remove all tags for entity
        $this->get("icaplyon1_simpletag.manager")->removeAllTags($entity);
        
        //Remove entity from database
        $em->remove($entity);
        $em->flush();
    }

    return $this->redirect($this->generateUrl('taggableentity'));
}

// ...

获取标签

在您的管理器(PHP)中

获取实体的所有标签

$this->get("icaplyon1_simpletag.manager")->getTags($entity);

在 Twig 模板中

要获取与对象关联的标签,已创建了一个 Twig 扩展,如下所示使用它

{{ entity_tags(entity) }}

获取所有存储的标签

您可以获取所有存储的标签,例如用于自动完成

使用 PHP

$this->get("icaplyon1_simpletag.manager")->getAllTags();

使用 Twig

{{ all_tags() }}