Ported Blog\Post entity and repo.

This commit is contained in:
Alexey Skobkin 2023-03-19 17:43:05 +03:00
parent bcc2c609c4
commit 3fd76ff0ab
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
2 changed files with 66 additions and 105 deletions

View file

@ -1,109 +1,60 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Entity\Blogs; namespace App\Entity\Blog;
use App\Entity\User;
use App\Repository\Blog\TagRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use src\PointToolsBundle\Entity\Blogs\File;
use src\PointToolsBundle\Entity\Blogs\PostTag;
use src\PointToolsBundle\Entity\User;
use src\PointToolsBundle\Entity\Blogs\Comment;
/** #[ORM\Entity(repositoryClass: TagRepository::class)]
* @ORM\Table(name="posts", schema="posts", indexes={ #[ORM\HasLifecycleCallbacks]
* @ORM\Index(name="idx_post_created_at", columns={"created_at"}), #[ORM\Table(name: 'tags', schema: 'posts')]
* @ORM\Index(name="idx_post_private", columns={"private"}),
* })
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository")
* @ORM\HasLifecycleCallbacks
*/
class Post class Post
{ {
public const TYPE_POST = 'post'; public const TYPE_POST = 'post';
public const TYPE_FEED = 'feed'; public const TYPE_FEED = 'feed';
/** #[ORM\Id]
* @var string #[ORM\Column(name: 'id', type: 'text')]
* private string $id;
* @ORM\Column(name="id", type="text")
* @ORM\Id
*/
private $id;
/** #[ORM\Column(name: 'text', type: 'text')]
* @var string private string $text;
*
* @ORM\Column(name="text", type="text")
*/
private $text;
/** #[ORM\Column(name: 'created_at', type: 'datetime')]
* @var \DateTime private \DateTime $createdAt;
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/** #[ORM\Column(name: 'updated_at', type: 'datetime', nullable: true)]
* @var \DateTime private ?\DateTime $updatedAt;
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/** #[ORM\Column(name: 'type', type: 'string', length: 6)]
* @var string private string $type = self::TYPE_POST;
*
* @ORM\Column(name="type", type="string", length=6)
*/
private $type = self::TYPE_POST;
/** #[ORM\Column(name: 'private', type: 'boolean', nullable: true)]
* @var bool private bool $private;
*
* @ORM\Column(name="private", type="boolean", nullable=true)
*/
private $private;
/** #[ORM\Column(name: 'is_deleted', type: 'boolean')]
* @var bool private bool $deleted = false;
*
* @ORM\Column(name="is_deleted", type="boolean")
*/
private $deleted = false;
/** #[ORM\ManyToOne(targetEntity: User::class)]
* @var User #[ORM\JoinColumn(name: 'author')]
* private User $author;
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\User")
* @ORM\JoinColumn(name="author")
*/
private $author;
/** /** @var ArrayCollection|File[] */
* @var File[]|ArrayCollection #[ORM\ManyToMany(targetEntity: File::class, cascade: ['persist'], fetch: 'EXTRA_LAZY')]
* #[ORM\JoinTable(name: 'posts_files', schema: 'posts')]
* @ORM\ManyToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\File", fetch="EXTRA_LAZY", cascade={"persist"}) #[ORM\JoinColumn(name: 'post_id')]
* @ORM\JoinTable(name="posts_files", schema="posts", #[ORM\InverseJoinColumn(name: 'file_id')]
* joinColumns={@ORM\JoinColumn(name="post_id")},
* inverseJoinColumns={@ORM\JoinColumn(name="file_id")}
* )
*/
private $files; private $files;
/** #[ORM\OneToMany(mappedBy: 'post', targetEntity: PostTag::class, cascade: ['persist'], fetch: 'EXTRA_LAZY', orphanRemoval: true)]
* @var PostTag[]|ArrayCollection private ArrayCollection $postTags;
*
* @ORM\OneToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\PostTag", mappedBy="post", fetch="EXTRA_LAZY", cascade={"persist"}, orphanRemoval=true)
*/
private $postTags;
/** /** @var ArrayCollection|Comment[] */
* @var Comment[]|ArrayCollection #[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'post', cascade: ['persist'], )]
* private ArrayCollection $comments;
* @ORM\OneToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment", mappedBy="post", cascade={"persist"})
*/
private $comments;
public function __construct(string $id, User $author, \DateTime $createdAt, string $type) public function __construct(string $id, User $author, \DateTime $createdAt, string $type)
@ -118,9 +69,7 @@ class Post
$this->comments = new ArrayCollection(); $this->comments = new ArrayCollection();
} }
/** #[ORM\PreUpdate]
* @ORM\PreUpdate
*/
public function preUpdate(): void public function preUpdate(): void
{ {
$this->updatedAt = new \DateTime(); $this->updatedAt = new \DateTime();
@ -175,9 +124,7 @@ class Post
$this->files->removeElement($files); $this->files->removeElement($files);
} }
/** /** @return File[]|ArrayCollection */
* @return File[]|ArrayCollection
*/
public function getFiles(): iterable public function getFiles(): iterable
{ {
return $this->files; return $this->files;
@ -195,9 +142,7 @@ class Post
$this->postTags->removeElement($tag); $this->postTags->removeElement($tag);
} }
/** /** @return PostTag[]|ArrayCollection */
* @return PostTag[]|ArrayCollection
*/
public function getPostTags(): iterable public function getPostTags(): iterable
{ {
return $this->postTags; return $this->postTags;
@ -250,9 +195,7 @@ class Post
$this->comments->removeElement($comment); $this->comments->removeElement($comment);
} }
/** /** @return Comment[]|ArrayCollection */
* @return Comment[]|ArrayCollection
*/
public function getComments(): iterable public function getComments(): iterable
{ {
return $this->comments; return $this->comments;

View file

@ -1,21 +1,39 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Repository\Blogs; namespace App\Repository\Blog;
use Doctrine\ORM\EntityRepository; use App\Entity\Blog\Post;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use src\PointToolsBundle\Entity\Blogs\Post; use Doctrine\Persistence\ManagerRegistry;
class PostRepository extends EntityRepository /**
* @extends ServiceEntityRepository<Post>
*
* @method Post|null find($id, $lockMode = null, $lockVersion = null)
* @method Post|null findOneBy(array $criteria, array $orderBy = null)
* @method Post[] findAll()
* @method Post[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PostRepository extends ServiceEntityRepository
{ {
public function add(Post $entity): void public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Post::class);
}
public function save(Post $entity, bool $flush = false): void
{ {
$this->getEntityManager()->persist($entity); $this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
} }
public function getPostWithComments(string $postId): ?Post public function getPostWithComments(string $postId): ?Post
{ {
/** @var QueryBuilder $qb */
$qb = $this->createQueryBuilder('p'); $qb = $this->createQueryBuilder('p');
return $qb return $qb
@ -27,7 +45,7 @@ class PostRepository extends EntityRepository
->orderBy('c.number', 'asc') ->orderBy('c.number', 'asc')
->setParameter('post_id', $postId) ->setParameter('post_id', $postId)
->getQuery()->getOneOrNullResult() ->getQuery()->getOneOrNullResult()
; ;
} }
public function createPublicFeedPostsQuery(): QueryBuilder public function createPublicFeedPostsQuery(): QueryBuilder
@ -43,6 +61,6 @@ class PostRepository extends EntityRepository
->where('p.private = FALSE') ->where('p.private = FALSE')
->andWhere('pa.public = TRUE') ->andWhere('pa.public = TRUE')
->orderBy('p.createdAt', 'desc') ->orderBy('p.createdAt', 'desc')
; ;
} }
} }