public function getNewGroupsByUser(User $user)
 {
     $groupOfUserIds = $user->getGroupsIds();
     $qb = $this->getEntityManager()->createQueryBuilder();
     $limitDate = new \DateTime('NOW');
     $limitDate->sub(new \DateInterval('P7D'));
     $qb->select('g')->from('CivixCoreBundle:Group', 'g')->leftJoin('g.users', 'u')->where('g.groupType = :type')->andWhere('g.createdAt > :limit_date')->setParameters(array('type' => Group::GROUP_TYPE_COMMON, 'limit_date' => $limitDate))->orderBy('g.createdAt', 'DESC');
     if (!empty($groupOfUserIds)) {
         $qb->andWhere('g.id NOT IN (:ids)')->setParameter('ids', $groupOfUserIds);
     }
     return $qb->getQuery()->getResult();
 }
 public function findByUser(User $user, \DateTime $start)
 {
     $districtsIds = $user->getDistrictsIds();
     $groupsIds = $user->getGroupsIds();
     $representativeIds = array();
     if (!empty($districtsIds)) {
         $qb = $this->getEntityManager()->createQueryBuilder();
         $representativeIds = $qb->select('r.id')->from('CivixCoreBundle:Representative', 'r')->where($qb->expr()->in('r.district', $districtsIds))->getQuery()->getArrayResult();
         $representativeIds = array_reduce($representativeIds, function ($ids, $item) {
             $ids[] = $item['id'];
             return $ids;
         }, array());
     }
     $representativeIds = empty($representativeIds) ? array(0) : $representativeIds;
     $groupsIds = empty($groupsIds) ? array(0) : $groupsIds;
     $qb = $this->getEntityManager()->createQueryBuilder();
     return $qb->select('a, r, gr, rs')->from('CivixCoreBundle:Announcement', 'a')->leftJoin('a.group', 'gr')->leftJoin('a.representative', 'r')->leftJoin('r.representativeStorage', 'rs')->where($qb->expr()->orX($qb->expr()->in('a.representative', $representativeIds), $qb->expr()->in('a.group', $groupsIds)))->andWhere('a.publishedAt > :start')->setParameter('start', $start->format('Y-m-d H:i:s'))->orderBy('a.publishedAt', 'DESC')->getQuery()->getResult();
 }
 public function getGroupsIds()
 {
     $this->__load();
     return parent::getGroupsIds();
 }