diff --git a/search2/search/expr.inc.php b/search2/search/expr.inc.php index 3df0f9e..f7ba4ae 100755 --- a/search2/search/expr.inc.php +++ b/search2/search/expr.inc.php @@ -1079,6 +1079,7 @@ class SQLQueryBuilder implements QueryBuilder if (array_key_exists($tablename, $this->used_tables)) { $this->used_tables[$tablename]++; + if (isset($expr->references)) ++$expr->references; } } } @@ -1244,7 +1245,18 @@ class SQLQueryBuilder implements QueryBuilder } if ($this->used_tables['document_fields_link'] > 0) { - $sql .= ' LEFT JOIN document_fields_link pdfl ON dmv.id=pdfl.metadata_version_id ' . "\n"; + // NOTE this is a bit of a hack, maybe? or maybe it really is the best way? + // ...what about using the loop below which checks the expression + // for a join table rather than this up here? - otherwise this only + // affects this particular query type - then again maybe that's what we want... + for ($i = 0; $i < $this->used_tables['document_fields_link']; ++$i) + { + if ($i > 0) $counter = $i; + else $counter = ''; + + $sql .= ' LEFT JOIN document_fields_link pdfl' . $counter . ' ' + . 'ON dmv.id=pdfl' . $counter . '.metadata_version_id ' . "\n"; + } } if ($this->used_tables['tag_words'] > 0) @@ -1374,7 +1386,6 @@ class SQLQueryBuilder implements QueryBuilder public function buildComplexQuery($expr) { -// print "building complex \n\n"; $this->exploreExprs($expr); $sql = $this->buildCoreSQL(); @@ -1396,7 +1407,6 @@ class SQLQueryBuilder implements QueryBuilder public function buildSimpleQuery($op, $group) { -// print "building simple \n\n"; $this->exploreGroup($group); $sql = $this->buildCoreSQL(); diff --git a/search2/search/fields/AnyMetadataField.inc.php b/search2/search/fields/AnyMetadataField.inc.php index 7ef2ffe..9531051 100755 --- a/search2/search/fields/AnyMetadataField.inc.php +++ b/search2/search/fields/AnyMetadataField.inc.php @@ -40,7 +40,7 @@ class AnyMetadataField extends DBFieldExpr { public $general_op = ExprOp::CONTAINS; - + public $references = 0; public function __construct() { @@ -48,6 +48,34 @@ class AnyMetadataField extends DBFieldExpr $this->setAlias('Metadata'); } + /* + * Overridden function to adjust table alias in cases of + * the document_fields_link table being included more than once + * + * NOTE this only works in conjunction with code in expr.inc.php which adds the left joins to the db query. + * I don't like this and think we should look for a way to make the table joining more generic + * such that it can be controlled via these classes and thereby contained as a unit. + */ + public function modifyName($name) + { + if ($this->references > 0) + { + static $count = 0; + if ($count >= $this->references) + { + $count = 0; + } + + if ((($pos = strpos($name, '.')) !== false) && ($count != 0)) + { + $name = substr($name, 0, $pos) . $count . substr($name, $pos); + } + ++$count; + } + + return $name; + } + public function getInputRequirements() { return array('value'=>array('type'=>FieldInputType::TEXT));