*/ class AudienceRepository extends CommonRepository { public function getTableAlias(): string { return 'pa'; } /** * The contact count of one segment: lead_lists_leads rows with manually_removed = 0. */ public function getSegmentContactCount(int $leadlistId): int { $count = $this->getEntityManager()->getConnection()->fetchOne( 'SELECT COUNT(ll.lead_id) FROM lead_lists_leads ll WHERE ll.leadlist_id = :leadlistId AND ll.manually_removed = 0', ['leadlistId' => $leadlistId] ); return (int) $count; } /** * Contact counts (lead_lists_leads rows with manually_removed = 0) for the segments of one * audience, keyed by segment id. Used by the list view to show "Segment (123)". * * @return array leadlist_id => contact count */ public function getSegmentContactCounts(int $audienceId): array { $rows = $this->getEntityManager()->getConnection()->fetchAllAssociative( 'SELECT s.leadlist_id, COUNT(ll.lead_id) AS cnt FROM initiative_audiences_segments s JOIN lead_lists_leads ll ON ll.leadlist_id = s.leadlist_id AND ll.manually_removed = 0 WHERE s.audience_id = :audienceId GROUP BY s.leadlist_id', ['audienceId' => $audienceId] ); $counts = []; foreach ($rows as $row) { $counts[(int) $row['leadlist_id']] = (int) $row['cnt']; } return $counts; } /** * Contact counts for the segments of every audience in the list view, keyed by audience id then * segment id. One query for the whole page. * * @param list $audienceIds * * @return array> audience_id => (leadlist_id => contact count) */ public function getSegmentContactCountsForAudiences(array $audienceIds): array { if ([] === $audienceIds) { return []; } $connection = $this->getEntityManager()->getConnection(); $rows = $connection->fetchAllAssociative( 'SELECT s.audience_id, s.leadlist_id, COUNT(ll.lead_id) AS cnt FROM initiative_audiences_segments s JOIN lead_lists_leads ll ON ll.leadlist_id = s.leadlist_id AND ll.manually_removed = 0 WHERE s.audience_id IN (:audienceIds) GROUP BY s.audience_id, s.leadlist_id', ['audienceIds' => $audienceIds], ['audienceIds' => Connection::PARAM_INT_ARRAY] ); $counts = []; foreach ($rows as $row) { $counts[(int) $row['audience_id']][(int) $row['leadlist_id']] = (int) $row['cnt']; } return $counts; } }