Download the PHP package damianociarla/tag-bundle without Composer
On this page you can find all versions of the php package damianociarla/tag-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download damianociarla/tag-bundle
More information about damianociarla/tag-bundle
Files in damianociarla/tag-bundle
Package tag-bundle
Short Description DCSTagBundle adds tagging to your Symfony project, with the ability to associate tags with any number of different entities
License MIT
Homepage https://github.com/damianociarla/DCSTagBundle
Informations about the package tag-bundle
DCSTagBundle
DCSTagBundle adds tagging to your Symfony project, with the ability to associate tags with any number of different entities.
Installation
a) Download and install DCSTagBundle
To install DCSTagBundle run the following command
bash $ php composer.phar require damianociarla/tag-bundle
b) Enable the bundle
To enable it add the bundle instance in the kernel:
<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) Configure your application
# app/config/config.yml
dcs_tag:
db_driver: orm
model: Acme\TagBundle\Entity\Tag
4) Add relationships to your Entity classes
<?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) Use the "dcs_tag" form type
<?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) How to save tags
If you use the form you don't need the Tag model manager (dcs_tag.manager
) to persist the entity because it's already managed by DataTransformer, but if you want manually add a single tag to a collection, you can use the following code:
$tagManager = $this->container->get('dcs_tag.manager');
$post = new Post();
$post->addTag($tagManager->add('tag-to-add'));
//... persist the post object
The add
method inserts a new tag, if already doesn’t exists, otherwise it returns the found tag.