From 507a8a5721442a4e8435b9c700281ee35a507a38 Mon Sep 17 00:00:00 2001 From: "Gergely POLONKAI (W00d5t0ck)" Date: Sun, 16 Sep 2012 22:58:51 +0200 Subject: [PATCH] Added the Taggable interface to blog posts Signed-off-by: Gergely POLONKAI (W00d5t0ck) --- app/AppKernel.php | 1 + .../Version20120916225801.php | 32 +++++++++++++++++ app/config/config.yml | 7 +++- composer.json | 1 + .../FrontBundle/Entity/Post.php | 22 +++++++++++- .../FrontBundle/Entity/Tag.php | 34 ++++++++++++++++++ .../FrontBundle/Entity/Tagging.php | 36 +++++++++++++++++++ 7 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 app/DoctrineMigrations/Version20120916225801.php create mode 100644 src/GergelyPolonkai/FrontBundle/Entity/Tag.php create mode 100644 src/GergelyPolonkai/FrontBundle/Entity/Tagging.php diff --git a/app/AppKernel.php b/app/AppKernel.php index a166712..b2fdf98 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -24,6 +24,7 @@ class AppKernel extends Kernel new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(), new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(), new Ivory\CKEditorBundle\IvoryCKEditorBundle(), + new FPN\TagBundle\FPNTagBundle(), new GergelyPolonkai\FrontBundle\GergelyPolonkaiFrontBundle(), new GergelyPolonkai\GeshiBundle\GergelyPolonkaiGeshiBundle(), ); diff --git a/app/DoctrineMigrations/Version20120916225801.php b/app/DoctrineMigrations/Version20120916225801.php new file mode 100644 index 0000000..77e9974 --- /dev/null +++ b/app/DoctrineMigrations/Version20120916225801.php @@ -0,0 +1,32 @@ +abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); + + $this->addSql("CREATE TABLE tagging (id INT AUTO_INCREMENT NOT NULL, tag_id INT DEFAULT NULL, resource_type VARCHAR(50) NOT NULL, resource_id VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX IDX_A4AED123BAD26311 (tag_id), UNIQUE INDEX tagging_idx (tag_id, resource_type, resource_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB"); + $this->addSql("CREATE TABLE tags (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(50) NOT NULL, slug VARCHAR(50) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, UNIQUE INDEX UNIQ_6FBC94265E237E06 (name), UNIQUE INDEX UNIQ_6FBC9426989D9B62 (slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB"); + $this->addSql("ALTER TABLE tagging ADD CONSTRAINT FK_A4AED123BAD26311 FOREIGN KEY (tag_id) REFERENCES tags (id)"); + } + + public function down(Schema $schema) + { + // this down() migration is autogenerated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); + + $this->addSql("ALTER TABLE tagging DROP FOREIGN KEY FK_A4AED123BAD26311"); + $this->addSql("DROP TABLE tagging"); + $this->addSql("DROP TABLE tags"); + } +} diff --git a/app/config/config.yml b/app/config/config.yml index b41bb37..aebefc3 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -68,4 +68,9 @@ stof_doctrine_extensions: orm: default: sluggable: true - timestampable: true \ No newline at end of file + timestampable: true + +fpn_tag: + model: + tag_class: GergelyPolonkai\FrontBundle\Entity\Tag + tagging_class: GergelyPolonkai\FrontBundle\Entity\Tagging \ No newline at end of file diff --git a/composer.json b/composer.json index 113f508..f6a569a 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "easybook/geshi": "dev-master", "stof/doctrine-extensions-bundle": "dev-master", "doctrine/doctrine-migrations-bundle": "dev-master", + "fpn/tag-bundle": "dev-master", "egeloen/ckeditor-bundle": "dev-master", "gergelypolonkai/tcpdfbundle": "dev-master" }, diff --git a/src/GergelyPolonkai/FrontBundle/Entity/Post.php b/src/GergelyPolonkai/FrontBundle/Entity/Post.php index d166e70..1049db3 100644 --- a/src/GergelyPolonkai/FrontBundle/Entity/Post.php +++ b/src/GergelyPolonkai/FrontBundle/Entity/Post.php @@ -7,6 +7,7 @@ use Gedmo\Mapping\Annotation as GedmoORM; use Symfony\Component\Validator\Constraints as Assert; use Doctrine\Common\Collections\ArrayCollection; +use DoctrineExtensions\Taggable\Taggable; use GergelyPolonkai\FrontBundle\Entity\Comment; use GergelyPolonkai\FrontBundle\Entity\User; @@ -19,7 +20,7 @@ use GergelyPolonkai\FrontBundle\Entity\User; * @ORM\Entity * @ORM\Table(name="blog_posts") */ -class Post +class Post implements Taggable { /** * @var integer $id @@ -86,6 +87,8 @@ class Post */ private $comments; + private $tags; + /** * Constructor */ @@ -273,4 +276,21 @@ class Post { return $this->comments; } + + public function getTags() + { + $this->tags = $this->tags ?: new ArrayCollection(); + + return $this->tags; + } + + public function getTaggableType() + { + return 'gergelypolonkaifront_post'; + } + + public function getTaggableId() + { + return $this->id; + } } diff --git a/src/GergelyPolonkai/FrontBundle/Entity/Tag.php b/src/GergelyPolonkai/FrontBundle/Entity/Tag.php new file mode 100644 index 0000000..944be54 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Entity/Tag.php @@ -0,0 +1,34 @@ +