1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
7
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; |
8
|
|
|
|
9
|
|
|
class PostRepository extends EntityRepository |
10
|
1 |
|
{ |
11
|
|
|
public function add(Post $entity): void |
12
|
|
|
{ |
13
|
1 |
|
$this->getEntityManager()->persist($entity); |
14
|
|
|
} |
15
|
1 |
|
|
16
|
1 |
|
public function getPostWithComments(string $postId): ?Post |
17
|
1 |
|
{ |
18
|
1 |
|
/** @var QueryBuilder $qb */ |
19
|
1 |
|
$qb = $this->createQueryBuilder('p'); |
20
|
1 |
|
|
21
|
1 |
|
return $qb |
22
|
|
|
->select(['p', 'c', 'a']) |
23
|
|
|
->leftJoin('p.comments', 'c') |
24
|
|
|
// @todo Optimize https://ocramius.github.io/blog/doctrine-orm-optimization-hydration/ |
25
|
|
|
->leftJoin('c.author', 'a') |
26
|
|
|
->where($qb->expr()->eq('p.id', ':post_id')) |
27
|
|
|
->orderBy('c.number', 'asc') |
28
|
|
|
->setParameter('post_id', $postId) |
29
|
|
|
->getQuery()->getOneOrNullResult() |
30
|
|
|
; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function createPublicFeedPostsQuery(): QueryBuilder |
34
|
|
|
{ |
35
|
|
|
$qb = $this->createQueryBuilder('p'); |
36
|
|
|
|
37
|
|
|
return $qb |
38
|
|
|
// @todo optimize hydration |
39
|
|
|
->select(['p', 'pa', 'pt', 'pf']) |
40
|
|
|
->innerJoin('p.author', 'pa') |
41
|
|
|
->leftJoin('p.postTags', 'pt') |
42
|
|
|
->leftJoin('p.files', 'pf') |
43
|
|
|
->where('p.private = FALSE') |
44
|
|
|
->andWhere('pa.public = TRUE') |
45
|
|
|
; |
46
|
|
|
} |
47
|
|
|
} |