diff --git a/migrations/Version20210530161844.php b/migrations/Version20210530161844.php new file mode 100644 index 0000000..28533ff --- /dev/null +++ b/migrations/Version20210530161844.php @@ -0,0 +1,41 @@ +<?php + +declare(strict_types=1); + +namespace DoctrineMigrations; + +use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; + +/** + * Auto-generated Migration: Please modify to your needs! + */ +final class Version20210530161844 extends AbstractMigration +{ + public function getDescription(): string + { + return ''; + } + + public function up(Schema $schema): void + { + // this up() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE TABLE blog (id INT AUTO_INCREMENT NOT NULL, author_id INT NOT NULL, edited_by_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, teaser LONGTEXT DEFAULT NULL, teaser_image VARCHAR(255) DEFAULT NULL, content LONGTEXT NOT NULL, created_at DATETIME NOT NULL, edited_at DATETIME DEFAULT NULL, edit_reason VARCHAR(255) DEFAULT NULL, INDEX IDX_C0155143F675F31B (author_id), INDEX IDX_C0155143DD7B2EBC (edited_by_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE blog_section (blog_id INT NOT NULL, section_id INT NOT NULL, INDEX IDX_C185C76CDAE07E97 (blog_id), INDEX IDX_C185C76CD823E37A (section_id), PRIMARY KEY(blog_id, section_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE section (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, description VARCHAR(255) DEFAULT NULL, teaser_image VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE blog ADD CONSTRAINT FK_C0155143F675F31B FOREIGN KEY (author_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE blog ADD CONSTRAINT FK_C0155143DD7B2EBC FOREIGN KEY (edited_by_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE blog_section ADD CONSTRAINT FK_C185C76CDAE07E97 FOREIGN KEY (blog_id) REFERENCES blog (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE blog_section ADD CONSTRAINT FK_C185C76CD823E37A FOREIGN KEY (section_id) REFERENCES section (id) ON DELETE CASCADE'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE blog_section DROP FOREIGN KEY FK_C185C76CDAE07E97'); + $this->addSql('ALTER TABLE blog_section DROP FOREIGN KEY FK_C185C76CD823E37A'); + $this->addSql('DROP TABLE blog'); + $this->addSql('DROP TABLE blog_section'); + $this->addSql('DROP TABLE section'); + } +} diff --git a/migrations/Version20210530162315.php b/migrations/Version20210530162315.php new file mode 100644 index 0000000..a12175c --- /dev/null +++ b/migrations/Version20210530162315.php @@ -0,0 +1,34 @@ +<?php + +declare(strict_types=1); + +namespace DoctrineMigrations; + +use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; + +/** + * Auto-generated Migration: Please modify to your needs! + */ +final class Version20210530162315 extends AbstractMigration +{ + public function getDescription(): string + { + return ''; + } + + public function up(Schema $schema): void + { + // this up() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE TABLE comment (id INT AUTO_INCREMENT NOT NULL, blog_id INT NOT NULL, author_id INT NOT NULL, edited_by_id INT DEFAULT NULL, title VARCHAR(255) DEFAULT NULL, content LONGTEXT NOT NULL, created_at DATETIME NOT NULL, edited_at DATETIME DEFAULT NULL, edit_reason VARCHAR(255) DEFAULT NULL, INDEX IDX_9474526CDAE07E97 (blog_id), INDEX IDX_9474526CF675F31B (author_id), INDEX IDX_9474526CDD7B2EBC (edited_by_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526CDAE07E97 FOREIGN KEY (blog_id) REFERENCES blog (id)'); + $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526CF675F31B FOREIGN KEY (author_id) REFERENCES user (id)'); + $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526CDD7B2EBC FOREIGN KEY (edited_by_id) REFERENCES user (id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE comment'); + } +} diff --git a/src/Entity/Blog.php b/src/Entity/Blog.php index 30eebfe..c85c376 100644 --- a/src/Entity/Blog.php +++ b/src/Entity/Blog.php @@ -70,9 +70,15 @@ class Blog */ private $editReason; + /** + * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="blog") + */ + private $comments; + public function __construct() { $this->section = new ArrayCollection(); + $this->comments = new ArrayCollection(); } public function getId(): ?int @@ -211,4 +217,34 @@ class Blog return $this; } + + /** + * @return Collection|Comment[] + */ + public function getComments(): Collection + { + return $this->comments; + } + + public function addComment(Comment $comment): self + { + if (!$this->comments->contains($comment)) { + $this->comments[] = $comment; + $comment->setBlog($this); + } + + return $this; + } + + public function removeComment(Comment $comment): self + { + if ($this->comments->removeElement($comment)) { + // set the owning side to null (unless already changed) + if ($comment->getBlog() === $this) { + $comment->setBlog(null); + } + } + + return $this; + } } diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php new file mode 100644 index 0000000..b055743 --- /dev/null +++ b/src/Entity/Comment.php @@ -0,0 +1,162 @@ +<?php + +namespace App\Entity; + +use App\Repository\CommentRepository; +use Doctrine\ORM\Mapping as ORM; + +/** + * @ORM\Entity(repositoryClass=CommentRepository::class) + */ +class Comment +{ + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + */ + private $id; + + /** + * @ORM\ManyToOne(targetEntity=Blog::class, inversedBy="comments") + * @ORM\JoinColumn(nullable=false) + */ + private $blog; + + /** + * @ORM\Column(type="string", length=255, nullable=true) + */ + private $title; + + /** + * @ORM\Column(type="text") + */ + private $content; + + /** + * @ORM\ManyToOne(targetEntity=User::class, inversedBy="comments") + * @ORM\JoinColumn(nullable=false) + */ + private $author; + + /** + * @ORM\Column(type="datetime") + */ + private $createdAt; + + /** + * @ORM\Column(type="datetime", nullable=true) + */ + private $editedAt; + + /** + * @ORM\ManyToOne(targetEntity=User::class) + */ + private $editedBy; + + /** + * @ORM\Column(type="string", length=255, nullable=true) + */ + private $editReason; + + public function getId(): ?int + { + return $this->id; + } + + public function getBlog(): ?Blog + { + return $this->blog; + } + + public function setBlog(?Blog $blog): self + { + $this->blog = $blog; + + return $this; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(?string $title): self + { + $this->title = $title; + + return $this; + } + + public function getContent(): ?string + { + return $this->content; + } + + public function setContent(string $content): self + { + $this->content = $content; + + return $this; + } + + public function getAuthor(): ?User + { + return $this->author; + } + + public function setAuthor(?User $author): self + { + $this->author = $author; + + return $this; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(\DateTimeInterface $createdAt): self + { + $this->createdAt = $createdAt; + + return $this; + } + + public function getEditedAt(): ?\DateTimeInterface + { + return $this->editedAt; + } + + public function setEditedAt(?\DateTimeInterface $editedAt): self + { + $this->editedAt = $editedAt; + + return $this; + } + + public function getEditedBy(): ?User + { + return $this->editedBy; + } + + public function setEditedBy(?User $editedBy): self + { + $this->editedBy = $editedBy; + + return $this; + } + + public function getEditReason(): ?string + { + return $this->editReason; + } + + public function setEditReason(?string $editReason): self + { + $this->editReason = $editReason; + + return $this; + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index 479add4..9c93efc 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -66,9 +66,15 @@ class User implements UserInterface */ private $blogs; + /** + * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="author") + */ + private $comments; + public function __construct() { $this->blogs = new ArrayCollection(); + $this->comments = new ArrayCollection(); } public function getId(): ?int @@ -236,4 +242,34 @@ class User implements UserInterface return $this; } + + /** + * @return Collection|Comment[] + */ + public function getComments(): Collection + { + return $this->comments; + } + + public function addComment(Comment $comment): self + { + if (!$this->comments->contains($comment)) { + $this->comments[] = $comment; + $comment->setAuthor($this); + } + + return $this; + } + + public function removeComment(Comment $comment): self + { + if ($this->comments->removeElement($comment)) { + // set the owning side to null (unless already changed) + if ($comment->getAuthor() === $this) { + $comment->setAuthor(null); + } + } + + return $this; + } } diff --git a/src/Repository/CommentRepository.php b/src/Repository/CommentRepository.php new file mode 100644 index 0000000..25dc4de --- /dev/null +++ b/src/Repository/CommentRepository.php @@ -0,0 +1,50 @@ +<?php + +namespace App\Repository; + +use App\Entity\Comment; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\Persistence\ManagerRegistry; + +/** + * @method Comment|null find($id, $lockMode = null, $lockVersion = null) + * @method Comment|null findOneBy(array $criteria, array $orderBy = null) + * @method Comment[] findAll() + * @method Comment[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class CommentRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Comment::class); + } + + // /** + // * @return Comment[] Returns an array of Comment objects + // */ + /* + public function findByExampleField($value) + { + return $this->createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('c.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Comment + { + return $this->createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/src/Security/AppAuthenticator.php b/src/Security/AppAuthenticator.php index 3ef67ca..f5c02cc 100644 --- a/src/Security/AppAuthenticator.php +++ b/src/Security/AppAuthenticator.php @@ -176,18 +176,7 @@ class AppAuthenticator extends AbstractFormLoginAuthenticator implements Passwor /* -blog: - - -editedby => user -editreason - - comment: - -blog => blog -title, -content, author => user, createdAt, editedAt,