Comments primary key changed to simple. PostTag entity added. Blogs migration.

This commit is contained in:
Alexey Skobkin 2016-03-24 01:01:18 +03:00
parent 626a823233
commit e95b05f854
6 changed files with 334 additions and 55 deletions

View File

@ -0,0 +1,70 @@
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160324005746 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('CREATE SCHEMA IF NOT EXISTS posts');
$this->addSql('CREATE SEQUENCE posts.comments_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE posts.tags_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE posts.posts_tags_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE posts.comments (id INT NOT NULL, post_id VARCHAR(16) DEFAULT NULL, author_id INT DEFAULT NULL, parent_id INT DEFAULT NULL, text TEXT NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, is_rec BOOLEAN NOT NULL, is_deleted BOOLEAN NOT NULL, number SMALLINT NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_628999754B89032C ON posts.comments (post_id)');
$this->addSql('CREATE INDEX IDX_62899975F675F31B ON posts.comments (author_id)');
$this->addSql('CREATE INDEX IDX_62899975727ACA70 ON posts.comments (parent_id)');
$this->addSql('CREATE INDEX idx_comment_created_at ON posts.comments (created_at)');
$this->addSql('CREATE TABLE posts.posts (id VARCHAR(16) NOT NULL, author INT DEFAULT NULL, text TEXT NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, type VARCHAR(6) NOT NULL, private BOOLEAN DEFAULT NULL, is_deleted BOOLEAN NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_FA243F8FBDAFD8C8 ON posts.posts (author)');
$this->addSql('CREATE INDEX idx_post_created_at ON posts.posts (created_at)');
$this->addSql('CREATE INDEX idx_post_private ON posts.posts (private)');
$this->addSql('CREATE TABLE posts.tags (id INT NOT NULL, text VARCHAR(128) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_115E64E03B8BA7C7 ON posts.tags (text)');
$this->addSql('CREATE TABLE posts.posts_tags (id INT NOT NULL, post_id VARCHAR(16) DEFAULT NULL, tag_id INT DEFAULT NULL, text VARCHAR(128) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_7870CC824B89032C ON posts.posts_tags (post_id)');
$this->addSql('CREATE INDEX IDX_7870CC82BAD26311 ON posts.posts_tags (tag_id)');
$this->addSql('ALTER TABLE posts.comments ADD CONSTRAINT FK_628999754B89032C FOREIGN KEY (post_id) REFERENCES posts.posts (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE posts.comments ADD CONSTRAINT FK_62899975F675F31B FOREIGN KEY (author_id) REFERENCES users.users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE posts.comments ADD CONSTRAINT FK_62899975727ACA70 FOREIGN KEY (parent_id) REFERENCES posts.comments (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE posts.posts ADD CONSTRAINT FK_FA243F8FBDAFD8C8 FOREIGN KEY (author) REFERENCES users.users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE posts.posts_tags ADD CONSTRAINT FK_7870CC824B89032C FOREIGN KEY (post_id) REFERENCES posts.posts (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE posts.posts_tags ADD CONSTRAINT FK_7870CC82BAD26311 FOREIGN KEY (tag_id) REFERENCES posts.tags (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('ALTER TABLE posts.comments DROP CONSTRAINT FK_62899975727ACA70');
$this->addSql('ALTER TABLE posts.comments DROP CONSTRAINT FK_628999754B89032C');
$this->addSql('ALTER TABLE posts.posts_tags DROP CONSTRAINT FK_7870CC824B89032C');
$this->addSql('ALTER TABLE posts.posts_tags DROP CONSTRAINT FK_7870CC82BAD26311');
$this->addSql('DROP SEQUENCE posts.comments_id_seq CASCADE');
$this->addSql('DROP SEQUENCE posts.tags_id_seq CASCADE');
$this->addSql('DROP SEQUENCE posts.posts_tags_id_seq CASCADE');
$this->addSql('DROP TABLE posts.comments');
$this->addSql('DROP TABLE posts.posts');
$this->addSql('DROP TABLE posts.tags');
$this->addSql('DROP TABLE posts.posts_tags');
$this->addSql('DROP SCHEMA IF EXISTS posts');
}
}

View File

@ -2,6 +2,7 @@
namespace Skobkin\Bundle\PointToolsBundle\Entity\Blogs;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Skobkin\Bundle\PointToolsBundle\Entity\User;
@ -16,10 +17,11 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User;
class Comment
{
/**
* @var integer
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
@ -54,16 +56,22 @@ class Comment
/**
* @var Post
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post", inversedBy="comments")
* @ORM\JoinColumn(name="post_id")
*/
private $post;
/**
* @var int
*
* @ORM\Column(name="number", type="smallint")
*/
private $number;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\User")
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\User", fetch="EAGER")
* @ORM\JoinColumn(name="author_id")
*/
private $author;
@ -71,39 +79,22 @@ class Comment
/**
* @var Comment|null
*
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment")
* @ORM\JoinColumn(name="to_comment_id", nullable=true)
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", nullable=true)
*/
private $toComment;
private $parent;
/**
* @param int $id
* @param Post $post
* @param User $author
* @param Comment|null $toComment
* @param string $text
* @param \DateTime $createdAt
* @param bool $isRec
* @var Comment[]
*
* @ORM\OneToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment", fetch="EXTRA_LAZY", mappedBy="parent")
*/
public function __construct($id, Post $post, User $author, Comment $toComment = null, $text, \DateTime $createdAt, $isRec)
{
if (!is_numeric($id)) {
throw new \InvalidArgumentException('$id must be an integer');
}
if (!is_bool($isRec)) {
throw new \InvalidArgumentException('$isRec must be boolean');
}
if (!is_string($text)) {
throw new \InvalidArgumentException('$text must be a string');
}
private $children;
$this->id = (int)$id;
$this->post = $post;
$this->author = $author;
$this->toComment = $toComment;
$this->text = $text;
$this->createdAt = $createdAt;
$this->rec = $isRec;
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
@ -214,6 +205,29 @@ class Comment
return $this;
}
/**
* Set number
*
* @param int $number
* @return Comment
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* @return int
*/
public function getNumber()
{
return $this->number;
}
/**
* @return User
*/
@ -236,18 +250,18 @@ class Comment
/**
* @return Comment
*/
public function getToComment()
public function getParent()
{
return $this->toComment;
return $this->parent;
}
/**
* @param Comment $toComment
* @param Comment $parent
* @return Comment
*/
public function setToComment($toComment)
public function setParent($parent)
{
$this->toComment = $toComment;
$this->parent = $parent;
return $this;
}
@ -284,4 +298,50 @@ class Comment
{
return $this->deleted;
}
/**
* Set id
*
* @param integer $id
* @return Comment
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Add children
*
* @param Comment $children
* @return Comment
*/
public function addChild(Comment $children)
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* @param Comment $children
*/
public function removeChild(Comment $children)
{
$this->children->removeElement($children);
}
/**
* Get children
*
* @return ArrayCollection
*/
public function getChildren()
{
return $this->children;
}
}

View File

@ -82,18 +82,14 @@ class Post
/**
* @var Tag[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag", fetch="EXTRA_LAZY")
* @ORM\JoinTable(name="posts.posts_tags",
* joinColumns={@ORM\JoinColumn(name="post_id")},
* inverseJoinColumns={@ORM\JoinColumn(name="tag_id")}
* )
* @ORM\OneToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\PostTag", mappedBy="post", fetch="EXTRA_LAZY", cascade={"persist"}, orphanRemoval=true)
*/
private $tags;
private $postTags;
/**
* @var Comment[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment", mappedBy="post")
* @ORM\OneToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment", mappedBy="post", cascade={"persist"})
*/
private $comments;
@ -106,7 +102,7 @@ class Post
{
$this->id = $id;
$this->tags = new ArrayCollection();
$this->postTags = new ArrayCollection();
$this->comments = new ArrayCollection();
}
@ -224,14 +220,14 @@ class Post
}
/**
* Add tags
* Add post tags
*
* @param Tag $tag
* @param PostTag $tag
* @return Post
*/
public function addTag(Tag $tag)
public function addPostTag(PostTag $tag)
{
$this->tags[] = $tag;
$this->postTags[] = $tag;
return $this;
}
@ -239,21 +235,21 @@ class Post
/**
* Remove tags
*
* @param Tag $tag
* @param PostTag $tag
*/
public function removeTag(Tag $tag)
public function removePostTag(PostTag $tag)
{
$this->tags->removeElement($tag);
$this->postTags->removeElement($tag);
}
/**
* Get tags
*
* @return Tag[]|ArrayCollection
* @return PostTag[]|ArrayCollection
*/
public function getTags()
public function getPostTags()
{
return $this->tags;
return $this->postTags;
}
/**

View File

@ -0,0 +1,144 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Entity\Blogs;
use Doctrine\ORM\Mapping as ORM;
/**
* PostTag
*
* @ORM\Table(name="posts.posts_tags", schema="posts")
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostTagRepository")
*/
class PostTag
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Post
*
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post", inversedBy="postTags")
* @ORM\JoinColumn(name="post_id", onDelete="CASCADE")
*/
private $post;
/**
* @var Tag
*
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag", fetch="EAGER")
* @ORM\JoinColumn(name="tag_id", onDelete="SET NULL")
*/
private $tag;
/**
* @var string
*
* @ORM\Column(name="text", type="string", length=128)
*/
private $text;
/**
* PostTag constructor.
*
* @param Tag $tag
*/
public function __construct(Tag $tag)
{
$this->tag = $tag;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set text
*
* @param string $text
* @return PostTag
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* @return string
*/
public function getOriginalTagText()
{
return $this->tag ? $this->tag->getText() : '';
}
/**
* Set post
*
* @param Post $post
* @return PostTag
*/
public function setPost(Post $post = null)
{
$this->post = $post;
return $this;
}
/**
* Get post
*
* @return Post
*/
public function getPost()
{
return $this->post;
}
/**
* Set tag
*
* @param Tag $tag
* @return PostTag
*/
public function setTag(Tag $tag = null)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* @return Tag
*/
public function getTag()
{
return $this->tag;
}
}

View File

@ -26,7 +26,7 @@ class Tag
/**
* @var string
*
* @ORM\Column(name="text", type="text", unique=true)
* @ORM\Column(name="text", type="string", length=128, unique=true)
*/
private $text;

View File

@ -0,0 +1,9 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs;
use Doctrine\ORM\EntityRepository;
class PostTagRepository extends EntityRepository
{
}