开发者

managment of many-to-many tags with Doctrine2

I am interested how tagging works. My idea so far:

I have three database tables Bookmarks id|title|uri|…, Tags id|title|… and bookmarks_tags (mxm, 3NF). My first test will be only a single-user system, so i have not to deal with Tags owned by specific users.

Storing a bookmark: uri (String) + tags (String, eg. Lorem Ipsum, Hello should result in two Tags: Lorem Ipsum and Hello).

Problem: Where and how should i create the missing Tags and loading the known ones?

Creating tags in the model is possible (see Bookmark::setTags() below). Loading and linking in the Model seems not possible, because the ORM is not available inside the class (or is there a static ressource for fetching the ORM? would this be recommended?).

I could load existing tags and create the tags inside Controller, but i assume tagging should be model's work.

I am using Symfony2 with Doctrine2.

Bookmark Class/Table

 * @ORM\Table()
 * @ORM\Entity(repositoryClass="X\BookmarksBundle\Entity\BookmarkRepository")
 */
class Bookmark
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /开发者_开发知识库**
     * @var datetime $created_at
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $created_at;

    /**
     * @var datetime $deleted_at
     *
     * @ORM\Column(name="deleted_at", type="datetime")
     */
    private $deleted_at;


    /** @ORM\ManyToMany(targetEntity="Tag", cascade={"persist", "remove"}) */
    private $tags;



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

    public function getTags () {
        if ($this->tags->isEmpty()) {
            return "NO TAGS";
        }

        // TODO load tags from db
        return "TODO: TAGS FOUND";
    }

    public function setTags ($tags) {
        // TODO create and load/link existing tags
        $tag = new Tag();
        $tag->setTitle("test tag");
        $this->tags->add($tag);
    }

    /* setters and getters for other private variables here */

Tag Class/Table

 * @ORM\Table()
 * @ORM\Entity(repositoryClass="X\BookmarksBundle\Entity\TagRepository")
 */
class Tag
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $title
     *
     * @ORM\Column(name="title", type="string", length=64)
     */
    private $title;

    /**
     * @var datetime $created_at
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $created_at;

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


    public function __construct () {
        $this->created_at = new \DateTime('now');
    }

        /* setters and getters for other private variables here */


When fetching entities from your database, Doctrine2 don't give you the POPO Entity but a "Proxy". This proxy has the ability to load missing elements from the database. Thus, you don't have to implement the logic of retrieving the missing data from the database.

Btw, you can also create this method:

public function addTag(Tag $tag)
{
    $this->tags->add($tag);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜