This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
CommentManager.php
63 lines (54 loc) · 1.83 KB
/
CommentManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\NewsBundle\Document;
use Sonata\CoreBundle\Model\BaseDocumentManager;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
use Sonata\NewsBundle\Model\CommentInterface;
use Sonata\NewsBundle\Model\CommentManagerInterface;
use Sonata\NewsBundle\Model\PostInterface;
class CommentManager extends BaseDocumentManager implements CommentManagerInterface
{
/**
* @param array $criteria
* @param int $page
* @param int $limit
* @param array $sort
*
* @return Pager
*/
public function getPager(array $criteria, $page, $limit = 10, array $sort = [])
{
$qb = $this->getDocumentManager()->getRepository($this->class)
->createQueryBuilder()
->sort('createdAt', 'desc');
$criteria['status'] = isset($criteria['status']) ? $criteria['status'] : CommentInterface::STATUS_VALID;
$qb->field('status')->equals($criteria['status']);
if (isset($criteria['postId'])) {
$qb->field('post')->equals($criteria['postId']);
}
$pager = new Pager(500); // no limit
$pager->setQuery(new ProxyQuery($qb));
$pager->setPage($page);
$pager->init();
return $pager;
}
/**
* Update the comments count.
*
* @param PostInterface $post
*/
public function updateCommentsCount(PostInterface $post = null)
{
$post->setCommentsCount($post->getCommentsCount() + 1);
$this->getDocumentManager()->persist($post);
$this->getDocumentManager()->flush();
}
}