diff --git a/data/migrations/Version20160820191203.php b/data/migrations/Version20160820191203.php new file mode 100644 index 00000000..10fb2b08 --- /dev/null +++ b/data/migrations/Version20160820191203.php @@ -0,0 +1,72 @@ +createTagsTable($schema); + $this->createShortUrlsInTagsTable($schema); + } + + protected function createTagsTable(Schema $schema) + { + $table = $schema->createTable('tags'); + $table->addColumn('id', Type::BIGINT, [ + 'unsigned' => true, + 'autoincrement' => true, + 'notnull' => true, + ]); + $table->addColumn('name', Type::STRING, [ + 'length' => 255, + 'notnull' => true, + ]); + $table->addUniqueIndex(['name']); + + $table->setPrimaryKey(['id']); + } + + protected function createShortUrlsInTagsTable(Schema $schema) + { + $table = $schema->createTable('short_urls_in_tags'); + $table->addColumn('short_url_id', Type::BIGINT, [ + 'unsigned' => true, + 'notnull' => true, + ]); + $table->addColumn('tag_id', Type::BIGINT, [ + 'unsigned' => true, + 'notnull' => true, + ]); + + $table->addForeignKeyConstraint('tags', ['tag_id'], ['id'], [ + 'onDelete' => 'CASCADE', + 'onUpdate' => 'RESTRICT', + ]); + $table->addForeignKeyConstraint('short_urls', ['short_url_id'], ['id'], [ + 'onDelete' => 'CASCADE', + 'onUpdate' => 'RESTRICT', + ]); + + $table->setPrimaryKey(['short_url_id', 'tag_id']); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + $schema->dropTable('short_urls_in_tags'); + $schema->dropTable('tags'); + } +} diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index 1100a363..f8964eb5 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -42,6 +42,16 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable * @ORM\OneToMany(targetEntity=Visit::class, mappedBy="shortUrl", fetch="EXTRA_LAZY") */ protected $visits; + /** + * @var Collection|Tag[] + * @ORM\ManyToMany(targetEntity=Tag::class, cascade={"persist"}) + * @ORM\JoinTable(name="short_urls_in_tags", joinColumns={ + * @ORM\JoinColumn(name="short_url_id", referencedColumnName="id") + * }, inverseJoinColumns={ + * @ORM\JoinColumn(name="tag_id", referencedColumnName="id") + * }) + */ + protected $tags; /** * ShortUrl constructor. @@ -125,6 +135,34 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable return $this; } + /** + * @return Collection|Tag[] + */ + public function getTags() + { + return $this->tags; + } + + /** + * @param Collection|Tag[] $tags + * @return $this + */ + public function setTags($tags) + { + $this->tags = $tags; + return $this; + } + + /** + * @param Tag $tag + * @return $this + */ + public function addTag(Tag $tag) + { + $this->tags->add($tag); + return $this; + } + /** * Specify data which should be serialized to JSON * @link http://php.net/manual/en/jsonserializable.jsonserialize.php diff --git a/module/Core/src/Entity/Tag.php b/module/Core/src/Entity/Tag.php new file mode 100644 index 00000000..7f10cfdf --- /dev/null +++ b/module/Core/src/Entity/Tag.php @@ -0,0 +1,40 @@ +name; + } + + /** + * @param string $name + * @return $this + */ + public function setName($name) + { + $this->name = $name; + return $this; + } +}