microservices-playground/api-comments

View on GitHub
src/AppBundle/Repository/Doctrine/DoctrineCommentRepository.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Foodlove\AppBundle\Repository\Doctrine;

use Foodlove\AppBundle\Entity\Entity;
use Foodlove\AppBundle\Repository\CommentRepository;
use Doctrine\ORM\EntityRepository;

class DoctrineCommentRepository extends EntityRepository implements CommentRepository
{
    public function add(Entity $comment)
    {
        $this->getEntityManager()->persist($comment);
        $this->getEntityManager()->flush();
    }

    public function remove(Entity $comment)
    {
        $this->getEntityManager()->remove($comment);
        $this->getEntityManager()->flush();
    }

    public function getById(int $id)
    {
        return $this->find($id);
    }

    public function getCommentsByPostId(int $postId): array
    {
        $queryBuilder = $this->createQueryBuilder('c');

        $queryBuilder
            ->leftJoin('c.mentions', 'm')
            ->where('c.postId = :postId')
            ->orderBy('c.createdAt', 'DESC')
            ->setParameter(':postId', $postId);

        return $queryBuilder->getQuery()->execute();
    }
}