code: public function countTotal($categoryId=null)
      {
          $qb = $this->getQueryBuilder()
              ->select('COUNT(p)');
  
          if(!is_null($categoryId)){
              $qb->join('p.categories', 'c')
                 ->where('c.id = :categoryId')
                 ->setParameter('categoryId', $categoryId);
          }
          
          return $qb->getQuery()->getSingleScalarResult();
      }
docs: Count the total of rows
  
  @return int