Skip to content

Commit

Permalink
Refactored the query to use Criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
shinde-rahul committed Jan 2, 2023
1 parent 4ebab80 commit b6bded4
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions app/bundles/LeadBundle/Entity/LeadCategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Mautic\LeadBundle\Entity;

use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Types;
use Mautic\CategoryBundle\Entity\Category;
use Mautic\CoreBundle\Entity\CommonRepository;

/**
Expand Down Expand Up @@ -36,7 +37,10 @@ public function getLeadCategories(Lead $lead)
*/
public function getSubscribedAndNewCategoryIds(Lead $lead, array $types): array
{
return $this->getLeadCategoriesMapping($lead, $types, true);
$criteria = Criteria::create()
->andWhere(Criteria::expr()->eq('manuallyRemoved', 1));

return $this->getLeadCategoriesMapping($lead, $types, $criteria);
}

/**
Expand All @@ -54,37 +58,34 @@ public function getNonAssociatedCategoryIdsForAContact(Lead $lead, array $types)
*
* @return array<int, int>
*/
private function getLeadCategoriesMapping(Lead $lead, array $types, bool $manuallyRemoved = false): array
private function getLeadCategoriesMapping(Lead $lead, array $types, Criteria $criteria = null): array
{
$qb = $this->_em->getConnection()->createQueryBuilder();

// Fetch the records from categories.
$parentQ = clone $qb;
$parentQ = $this->getEntityManager()->getRepository(Category::class)->createQueryBuilder('c');
$parentQ->select('c.id');
$parentQ->from(MAUTIC_TABLE_PREFIX.'categories', 'c');
$parentQ->where('c.is_published = 1');
$parentQ->andWhere($qb->expr()->in('c.bundle', ':bundles'));
$parentQ->where('c.isPublished = :isPublished');
$parentQ->setParameter('isPublished', 1);
$parentQ->andWhere($parentQ->expr()->in('c.bundle', ':bundles'));
$parentQ->setParameter('bundles', $types, Connection::PARAM_STR_ARRAY);

// Get the category ids for particular lead
$subQ = clone $qb;
$subQ->select('lc.category_id');
$subQ->from(MAUTIC_TABLE_PREFIX.'lead_categories', 'lc');
$subQ->where($qb->expr()->eq('lc.lead_id', ':leadId'));
$subQ->setParameter('leadId', $lead->getId(), Types::INTEGER);

if ($manuallyRemoved) {
$subQ->andWhere($qb->expr()->eq('lc.manually_removed', 1));
$subQ = $this->getEntityManager()->getRepository(LeadCategory::class)->createQueryBuilder('lc');
$subQ->select('IDENTITY(lc.category)');
$subQ->where($subQ->expr()->eq('lc.lead', ':leadId'));
$subQ->setParameter('leadId', $lead->getId());

if ($criteria instanceof Criteria) {
$subQ->addCriteria($criteria);
}

// Add sub-query
$parentQ->andWhere($qb->expr()->notIn('c.id', $subQ->getSQL()));
$parentQ->andWhere($parentQ->expr()->notIn('c.id', $subQ->getDQL()));

// Add sub-query parameter.
$parentQ->setParameter('leadId', $lead->getId(), Types::INTEGER);
foreach ($subQ->getParameters() as $parameter) {
$parentQ->setParameter($parameter->getName(), $parameter->getValue(), $parameter->getType());
}

$leadCategories = $parentQ->execute()
->fetchAllAssociative();
$leadCategories = $parentQ->getQuery()->getResult();

$leadCategoryList = [];
foreach ($leadCategories as $category) {
Expand Down

0 comments on commit b6bded4

Please sign in to comment.