2016-03-16 19:03:08 +00:00
|
|
|
<?php
|
|
|
|
|
2023-03-11 16:27:07 +00:00
|
|
|
namespace src\PointToolsBundle\Repository\Blogs;
|
2016-03-16 19:03:08 +00:00
|
|
|
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
2016-03-29 04:11:12 +00:00
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2023-03-11 16:27:07 +00:00
|
|
|
use src\PointToolsBundle\Entity\Blogs\Post;
|
2016-03-16 19:03:08 +00:00
|
|
|
|
|
|
|
class PostRepository extends EntityRepository
|
|
|
|
{
|
2017-01-12 22:58:52 +00:00
|
|
|
public function add(Post $entity): void
|
2017-01-11 16:17:52 +00:00
|
|
|
{
|
|
|
|
$this->getEntityManager()->persist($entity);
|
|
|
|
}
|
|
|
|
|
2017-01-12 22:58:52 +00:00
|
|
|
public function getPostWithComments(string $postId): ?Post
|
2016-03-29 04:11:12 +00:00
|
|
|
{
|
|
|
|
/** @var QueryBuilder $qb */
|
|
|
|
$qb = $this->createQueryBuilder('p');
|
2017-11-04 21:30:32 +00:00
|
|
|
|
2016-03-29 04:11:12 +00:00
|
|
|
return $qb
|
|
|
|
->select(['p', 'c', 'a'])
|
|
|
|
->leftJoin('p.comments', 'c')
|
2017-01-12 22:58:52 +00:00
|
|
|
// @todo Optimize https://ocramius.github.io/blog/doctrine-orm-optimization-hydration/
|
2016-03-29 04:11:12 +00:00
|
|
|
->leftJoin('c.author', 'a')
|
|
|
|
->where($qb->expr()->eq('p.id', ':post_id'))
|
2016-03-29 06:52:21 +00:00
|
|
|
->orderBy('c.number', 'asc')
|
2016-03-29 04:11:12 +00:00
|
|
|
->setParameter('post_id', $postId)
|
|
|
|
->getQuery()->getOneOrNullResult()
|
|
|
|
;
|
|
|
|
}
|
2017-11-04 21:30:32 +00:00
|
|
|
|
|
|
|
public function createPublicFeedPostsQuery(): QueryBuilder
|
|
|
|
{
|
|
|
|
$qb = $this->createQueryBuilder('p');
|
|
|
|
|
|
|
|
return $qb
|
|
|
|
// @todo optimize hydration
|
|
|
|
->select(['p', 'pa', 'pt', 'pf'])
|
|
|
|
->innerJoin('p.author', 'pa')
|
|
|
|
->leftJoin('p.postTags', 'pt')
|
|
|
|
->leftJoin('p.files', 'pf')
|
|
|
|
->where('p.private = FALSE')
|
|
|
|
->andWhere('pa.public = TRUE')
|
2017-11-04 21:46:54 +00:00
|
|
|
->orderBy('p.createdAt', 'desc')
|
2017-11-04 21:30:32 +00:00
|
|
|
;
|
|
|
|
}
|
2016-03-16 19:03:08 +00:00
|
|
|
}
|