wip.. inject personSearch results in similaritySearch to avoid duplicates results

error message :
---------------
[Semantical Error] line 0, col 13 near 'p.id) FROM ChillPersonBundle:Person':
Error: 'p' is used outside the scope of its declaration.
This commit is contained in:
Mat 2018-11-08 14:26:08 +01:00
parent 99ca79d86e
commit ecf3f541ef
3 changed files with 30 additions and 6 deletions

View File

@ -17,6 +17,7 @@ services:
- "@security.token_storage" - "@security.token_storage"
- "@chill.main.security.authorization.helper" - "@chill.main.security.authorization.helper"
- "@chill_main.paginator_factory" - "@chill_main.paginator_factory"
- '@chill.person.search_person'
calls: calls:
- ['setContainer', ["@service_container"]] - ['setContainer', ["@service_container"]]
tags: tags:

View File

@ -201,7 +201,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
* @param array $terms * @param array $terms
* @return \Doctrine\ORM\QueryBuilder * @return \Doctrine\ORM\QueryBuilder
*/ */
protected function createQuery(array $terms) public function createQuery(array $terms)
{ {
//get from cache //get from cache
$cacheKey = md5(serialize($terms)); $cacheKey = md5(serialize($terms));

View File

@ -50,6 +50,12 @@ class SimilarityPersonSearch extends AbstractSearch
const NAME = "person_similarity"; const NAME = "person_similarity";
/**
*
* @var PersonSearch
*/
private $personSearch;
/** /**
* SimilarityPersonSearch constructor. * SimilarityPersonSearch constructor.
@ -58,17 +64,20 @@ class SimilarityPersonSearch extends AbstractSearch
* @param TokenStorage $tokenStorage * @param TokenStorage $tokenStorage
* @param AuthorizationHelper $helper * @param AuthorizationHelper $helper
* @param PaginatorFactory $paginatorFactory * @param PaginatorFactory $paginatorFactory
* @param PersonSearch $personSearch
*/ */
public function __construct( public function __construct(
EntityManagerInterface $em, EntityManagerInterface $em,
TokenStorage $tokenStorage, TokenStorage $tokenStorage,
AuthorizationHelper $helper, AuthorizationHelper $helper,
PaginatorFactory $paginatorFactory) PaginatorFactory $paginatorFactory,
PersonSearch $personSearch)
{ {
$this->em = $em; $this->em = $em;
$this->user = $tokenStorage->getToken()->getUser(); $this->user = $tokenStorage->getToken()->getUser();
$this->helper = $helper; $this->helper = $helper;
$this->paginatorFactory = $paginatorFactory; $this->paginatorFactory = $paginatorFactory;
$this->personSearch = $personSearch;
// throw an error if user is not a valid user // throw an error if user is not a valid user
if (!$this->user instanceof \Chill\MainBundle\Entity\User) { if (!$this->user instanceof \Chill\MainBundle\Entity\User) {
@ -218,28 +227,42 @@ class SimilarityPersonSearch extends AbstractSearch
$qb = $this->em->createQueryBuilder(); $qb = $this->em->createQueryBuilder();
$qb->from('ChillPersonBundle:Person', 'p'); $qb->from('ChillPersonBundle:Person', 'simi');
if ($terms['_default'] !== '') { if ($terms['_default'] !== '') {
$grams = explode(' ', $terms['_default']); $grams = explode(' ', $terms['_default']);
foreach($grams as $key => $gram) { foreach($grams as $key => $gram) {
$qb->andWhere( $qb->andWhere('SIMILARITY(simi.fullnameCanonical, UNACCENT(LOWER(:default_'.$key.')) ) >= 0.15')
'SIMILARITY(p.fullnameCanonical, UNACCENT(LOWER(:default_'.$key.'))) >= 0.15')
->setParameter('default_'.$key, '%'.$gram.'%'); ->setParameter('default_'.$key, '%'.$gram.'%');
} }
///
//dump($this->personSearch->createQuery($terms)->addSelect('p.id')->getDQL());
$qb->andWhere($qb->expr()
->notIn(
'simi.id',
$this->personSearch->createQuery($terms)
->select('p.id')
->getDQL()
)
);
///
} }
//restraint center for security //restraint center for security
$reachableCenters = $this->helper->getReachableCenters($this->user, $reachableCenters = $this->helper->getReachableCenters($this->user,
new Role('CHILL_PERSON_SEE')); new Role('CHILL_PERSON_SEE'));
$qb->andWhere($qb->expr() $qb->andWhere($qb->expr()
->in('p.center', ':centers')) ->in('simi.center', ':centers'))
->setParameter('centers', $reachableCenters) ->setParameter('centers', $reachableCenters)
; ;
$this->_cacheQuery[$cacheKey] = $qb; $this->_cacheQuery[$cacheKey] = $qb;
dump($qb->getDQL());
return clone $qb; return clone $qb;
} }