Skip to content

Commit

Permalink
Allow inheritance for document and field entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Viburnum committed Jun 27, 2016
1 parent 2d86110 commit 3cb3eea
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* A Keen Metronic Admin Theme (http://www.keenthemes.com/) Bundle for Symfony2
* with some additional libraries and PecPlatform specific customization.
*/
class StingerSoftEntitySearchExtension extends Extension {
class StingerSoftDoctrineEntitySearchExtension extends Extension {

/**
*
Expand Down
8 changes: 8 additions & 0 deletions Model/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ public function setEntityId($entityId) {
public function getInternalEntityId() {
return $this->entityId;
}

public function __get($name) {
return $this->getFieldValue($name);
}

public function __isset($name) {
return $this->getFieldValue($name) !== null;
}

/**
* Creates a new field instance
Expand Down
25 changes: 22 additions & 3 deletions Model/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ abstract class Field {

protected $fieldName;

protected $fieldValue;
protected $internalFieldValue;

protected $serialized;

/**
* @var Document
Expand All @@ -38,11 +40,17 @@ public function setFieldName($fieldName) {
}

public function getFieldValue() {
return $this->fieldValue;
return $this->serialized ? unserialize($this->internalFieldValue) : $this->internalFieldValue;
}

public function setFieldValue($fieldValue) {
$this->fieldValue = $fieldValue;
if(is_scalar($fieldValue)){
$this->internalFieldValue = $fieldValue;
$this->serialized = false;
}else{
$this->internalFieldValue = serialize($fieldValue);
$this->serialized = true;
}
return $this;
}

Expand All @@ -54,5 +62,16 @@ public function setDocument(Document $document) {
$this->document = $document;
return $this;
}

public function getSerialized() {
return $this->serialized;
}

public function setSerialized($serialized) {
$this->serialized = $serialized;
return $this;
}



}
5 changes: 5 additions & 0 deletions Resources/config/doctrine/Document.orm.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
StingerSoft\DoctrineEntitySearchBundle\Entity\Document:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
length: 255
id:
id:
type: integer
Expand Down
11 changes: 10 additions & 1 deletion Resources/config/doctrine/Field.orm.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
StingerSoft\DoctrineEntitySearchBundle\Entity\Field:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
length: 255
id:
id:
type: integer
Expand All @@ -9,9 +14,13 @@ StingerSoft\DoctrineEntitySearchBundle\Entity\Field:
type: string
length: 512
nullable: false
fieldValue:
internalFieldValue:
column: fieldValue
type: text
nullable: false
serialized:
type: boolean
nullable: false
manyToOne:
document:
targetEntity: StingerSoft\DoctrineEntitySearchBundle\Entity\Document
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ services:
stinger_soft.doctrine_entity_search.search_service:
class: '%stinger_soft.doctrine_entity_search.search_service.class%'
arguments:
- documentClazz: '%stinger_soft.doctrine_entity_search.document.class%'
- fieldClazz: '%stinger_soft.doctrine_entity_search.field.class%'
- '%stinger_soft.doctrine_entity_search.document.class%'
- '%stinger_soft.doctrine_entity_search.field.class%'
20 changes: 10 additions & 10 deletions Services/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ public function autocomplete($search, $maxResults = 10) {
public function autocompleteOrm(EntityManager $em, $search, $maxResults = 10) {
$fieldRepos = $em->getRepository($this->fieldClazz);
$qb = $fieldRepos->createQueryBuilder('field');
$qb->select('field.fieldValue');
$qb->orWhere('field.fieldName = :titleFieldName AND field.fieldValue LIKE :term');
$qb->select('field.internalFieldValue');
$qb->orWhere('field.fieldName = :titleFieldName AND field.internalFieldValue LIKE :term');
$qb->setParameter('titleFieldName', \StingerSoft\EntitySearchBundle\Model\Document::FIELD_TITLE);
$qb->orWhere('field.fieldName = :contentFieldName AND field.fieldValue LIKE :term');
$qb->orWhere('field.fieldName = :contentFieldName AND field.internalFieldValue LIKE :term');
$qb->setParameter('contentFieldName', \StingerSoft\EntitySearchBundle\Model\Document::FIELD_CONTENT);
$qb->setParameter('term', $search . '%');
$qb->setParameter('term', '%'.$search . '%');
$iterator = $qb->getQuery()->iterate(null, \Doctrine\ORM\Query::HYDRATE_SCALAR);
$suggestions = array();
foreach($iterator as $res) {
$suggestions = array_merge($suggestions, array_filter(explode(' ', $res[0]['fieldValue']), function ($word) use ($search) {
$suggestions = array_merge($suggestions, array_filter(explode(' ', strip_tags($res[0]['internalFieldValue'])), function ($word) use ($search) {
return stripos($word, $search) === 0;
}));
}
Expand Down Expand Up @@ -151,7 +151,7 @@ protected function searchOrm(Query $query, EntityManager $em) {
$qb = $docRepos->createQueryBuilder('doc');
$qb->leftJoin('doc.internalFields', 'field');
foreach(self::$searchableFields as $field) {
$qb->orWhere('field.fieldName = :' . $field . 'FieldName AND field.fieldValue LIKE :term');
$qb->orWhere('field.fieldName = :' . $field . 'FieldName AND field.internalFieldValue LIKE :term');
$qb->setParameter($field . 'FieldName', $field);
}
$qb->setParameter('term', '%' . $term . '%');
Expand All @@ -162,7 +162,7 @@ protected function searchOrm(Query $query, EntityManager $em) {
foreach($query->getFacets() as $facetField => $facetValues) {
if($facetValues === null || count($facetValues) == 0)
continue;
$facetedQb->andWhere('field.fieldName = :' . $facetField . 'FacetFieldName AND field.fieldValue IN :' . $facetValues . 'FacetFieldValues');
$facetedQb->andWhere('field.fieldName = :' . $facetField . 'FacetFieldName AND field.internalFieldValue IN :' . $facetValues . 'FacetFieldValues');
$qb->setParameter($facetField . 'FacetFieldName', $facetField);
$qb->setParameter($facetField . 'FacetFieldValues', $facetValues);
}
Expand All @@ -173,10 +173,10 @@ protected function searchOrm(Query $query, EntityManager $em) {

$facetQb = clone $qb;
$facetQb->select('field.fieldName');
$facetQb->addSelect('field.fieldValue');
$facetQb->addSelect('field.internalFieldValue');
$facetQb->addSelect('COUNT(doc.id) as resultCount');
$facetQb->addGroupBy('field.fieldName');
$facetQb->addGroupBy('field.fieldValue');
$facetQb->addGroupBy('field.internalFieldValue');
$facetQb->orderBy('resultCount', 'DESC');

if($query->getUsedFacets() !== null) {
Expand All @@ -187,7 +187,7 @@ protected function searchOrm(Query $query, EntityManager $em) {
$facetSet = new FacetSetAdapter();
if($query->getUsedFacets() === null || count($query->getUsedFacets()) > 0) {
foreach($facetQb->getQuery()->getScalarResult() as $facetResult) {
$facetSet->addFacetValue($facetResult['fieldName'], $facetResult['fieldValue'], $facetResult['resultCount']);
$facetSet->addFacetValue($facetResult['fieldName'], $facetResult['internalFieldValue'], $facetResult['resultCount']);
}

if($query->getUsedFacets() === null || in_array('type', $query->getUsedFacets())) {
Expand Down
5 changes: 4 additions & 1 deletion Tests/Services/SearchServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,12 @@ protected function getMetadataDriverImplementation() {
realpath(__DIR__.'/../../Resources/config/doctrine/') => 'StingerSoft\DoctrineEntitySearchBundle\Entity',
);


$yamlDriver = new SimplifiedYamlDriver($namespaces);
$driver->addDriver($yamlDriver, 'StingerSoft\DoctrineEntitySearchBundle');
return $driver;
}

protected function getPaths(){
return array(realpath(__DIR__.'/../../Entity'));
}
}

0 comments on commit 3cb3eea

Please sign in to comment.