SS 4.0 - Fix SearchIntrospection and SearchIndex

This commit is contained in:
Brett Tasker 2017-04-22 21:30:29 +12:00
parent 0b7281b25e
commit faacb6bc47
2 changed files with 17 additions and 12 deletions

View File

@ -43,15 +43,11 @@ class SearchIntrospection
$classes = array_unique(array_merge($classes, array_values(ClassInfo::subclassesFor($class))));
}
$idx = array_search('SilverStripe\View\ViewableData', $classes);
$idx = array_search('SilverStripe\ORM\DataObject', $classes);
if ($idx !== false) {
array_splice($classes, 0, $idx+1);
}
$idx = array_search('SilverStripe\Core\Object', $classes);
if ($idx !== false) {
array_splice($classes, 0, $idx+1);
}
//@todo find another way to determine if a dataobject does not have a table
// if ($dataOnly) {
// foreach ($classes as $i => $class) {

View File

@ -10,6 +10,7 @@ use SilverStripe\Core\ClassInfo;
use SilverStripe\FullTextSearch\Search\SearchIntrospection;
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
use SilverStripe\FullTextSearch\Utils\MultipleArrayIterator;
use SilverStripe\ORM\Queries\SQLSelect;
/**
* SearchIndex is the base index class. Each connector will provide a subclass of this that
* provides search engine specific behavior.
@ -92,21 +93,23 @@ abstract class SearchIndex extends ViewableData
foreach (SearchIntrospection::hierarchy($source, $options['include_children']) as $dataclass) {
$singleton = singleton($dataclass);
$schema = DataObject::getSchema();
$className = $singleton->getClassName();
if ($hasOne = $singleton->hasOne($lookup)) {
if ($hasOne = $schema->hasOneComponent($className, $lookup)) {
$class = $hasOne;
$options['lookup_chain'][] = array(
'call' => 'method', 'method' => $lookup,
'through' => 'has_one', 'class' => $dataclass, 'otherclass' => $class, 'foreignkey' => "{$lookup}ID"
);
} elseif ($hasMany = $singleton->hasMany($lookup)) {
} elseif ($hasMany = $schema->hasManyComponent($className, $lookup)) {
$class = $hasMany;
$options['multi_valued'] = true;
$options['lookup_chain'][] = array(
'call' => 'method', 'method' => $lookup,
'through' => 'has_many', 'class' => $dataclass, 'otherclass' => $class, 'foreignkey' => $singleton->getRemoteJoinField($lookup, 'has_many')
'through' => 'has_many', 'class' => $dataclass, 'otherclass' => $class, 'foreignkey' => $schema->getRemoteJoinField($className, $lookup, 'has_many')
);
} elseif ($manyMany = $singleton->manyMany($lookup)) {
} elseif ($manyMany = $schema->manyManyComponent($className, $lookup)) {
$class = $manyMany[1];
$options['multi_valued'] = true;
$options['lookup_chain'][] = array(
@ -555,14 +558,20 @@ abstract class SearchIndex extends ViewableData
$ids = array($id);
foreach ($derivation['chain'] as $step) {
// Use TableName for queries
$tableName = DataObject::getSchema()->tableName($step['class']);
if ($step['through'] == 'has_one') {
$sql = new SQLQuery('"ID"', '"'.$step['class'].'"', '"'.$step['foreignkey'].'" IN ('.implode(',', $ids).')');
$sql = new SQLSelect('"ID"', '"'.$tableName.'"', '"'.$step['foreignkey'].'" IN ('.implode(',', $ids).')');
singleton($step['class'])->extend('augmentSQL', $sql);
$ids = $sql->execute()->column();
} elseif ($step['through'] == 'has_many') {
$sql = new SQLQuery('"'.$step['class'].'"."ID"', '"'.$step['class'].'"', '"'.$step['otherclass'].'"."ID" IN ('.implode(',', $ids).')');
$sql->addInnerJoin($step['otherclass'], '"'.$step['class'].'"."ID" = "'.$step['otherclass'].'"."'.$step['foreignkey'].'"');
// Use TableName for queries
$otherTableName = DataObject::getSchema()->tableName($step['otherclass']);
$sql = new SQLSelect('"'.$tableName.'"."ID"', '"'.$tableName.'"', '"'.$otherTableName.'"."ID" IN ('.implode(',', $ids).')');
$sql->addInnerJoin($otherTableName, '"'.$tableName.'"."ID" = "'.$otherTableName.'"."'.$step['foreignkey'].'"');
singleton($step['class'])->extend('augmentSQL', $sql);
$ids = $sql->execute()->column();