Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: agrintals
- * Date: 16/2/18
- * Time: 15:02 PM
- */
- namespace Dyninno\ReviewsAdminBundle\Service;
- class CountService // maybe better just include in same REP
- {
- private $em;
- public function __construct(EntityManagerInterface $em) // em
- {
- $this->em = $em;
- }
- // Where Should be Added in RevRepository
- /*
- Add New Review - if $params published == 1
- ***************************************************************************
- EditReview - if previous published was 0 and new is 1 OR if previou published was 1 and new is 0
- and IF review is published and there is made changes either in source or IN brands
- if there are made changes in source - call count public in sources
- if there are made changes in brand - call count public in brand
- **************************************************************************
- publishUnpublishReviews - call in all cases at the end...
- deleteReviews - call in all cases at the end...
- */
- public function countPublicInBrands(): void
- {
- // select all brands
- // in foreach dynamic query to select count
- // set this count to brand object
- $allBrands = $this->em->getRepository('ReviewsAdminBundle:Brands')->findAll();
- foreach ($allBrands as $oneBrand) {
- $queryBuilder = $this->em->createQueryBuilder();
- $countPublished = $queryBuilder->select('count(r.id)') // r or r.id
- ->from('ReviewsAdminBundle:Reviews', 'r')
- ->innerJoin('r.brand', 'b')
- ->where('b.id = :brand')
- ->andWhere('r.published = 1')
- ->setParameter('brand', $oneBrand)
- ->getQuery()
- ->getResult(); // getSingleScalar or Scalar ?
- $oneBrand->setCountPublished($countPublished);
- }
- }
- public function countPublicInGroups(): void
- {
- // select all groups
- // in foreach dynamic query to select count
- // set this count to group object
- $allGroups = $this->em->getRepository('ReviewsAdminBundle:Groups')->findAll();
- foreach ($allGroups as $oneGroup) {
- $queryBuilder = $this->em->createQueryBuilder();
- $idList = $this->getIdList($oneGroup);
- $exprSourceIds = $queryBuilder->expr()->in('r.source', $idList);
- $countPublished = $queryBuilder->select('count(r.id)')
- ->from('ReviewsAdminBundle:Reviews', 'r')
- ->leftJoin('r.source', 's')
- ->where($exprSourceIds)
- ->andWhere('r.published = 1')
- ->getQuery()
- ->getResult();
- $oneGroup->setCountPublished($countPublished);
- }
- }
- private function getIdList(Groups $group): array
- {
- $ids = [];
- $groupObject = $this->_em->getRepository('ReviewsAdminBundle:Groups')->findOneBy(['id' => $group]);
- foreach ($groupObject->getSourceList() as $source) {
- $ids[] = $source->getId();
- }
- return $ids;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement