Commit 686b2b7a5a8a8bc7dc37c40a835b1f0b6fcde498

Authored by Paul Barrett
1 parent fc3b3458

KTS-4214. Changed the behaviour of the search to allow multiple joins on a singl…

…e table when the document_fields_link table is involved - i.e. an AnyMetadata search.

Fixed

Search does not return any results if more than one metadata type is included in the search expression with an AND statement.

Committed by: Paul Barrett

Reviewed by: Megan Watson
search2/search/expr.inc.php
... ... @@ -1079,6 +1079,7 @@ class SQLQueryBuilder implements QueryBuilder
1079 1079 if (array_key_exists($tablename, $this->used_tables))
1080 1080 {
1081 1081 $this->used_tables[$tablename]++;
  1082 + if (isset($expr->references)) ++$expr->references;
1082 1083 }
1083 1084 }
1084 1085 }
... ... @@ -1244,7 +1245,18 @@ class SQLQueryBuilder implements QueryBuilder
1244 1245 }
1245 1246 if ($this->used_tables['document_fields_link'] > 0)
1246 1247 {
1247   - $sql .= ' LEFT JOIN document_fields_link pdfl ON dmv.id=pdfl.metadata_version_id ' . "\n";
  1248 + // NOTE this is a bit of a hack, maybe? or maybe it really is the best way?
  1249 + // ...what about using the loop below which checks the expression
  1250 + // for a join table rather than this up here? - otherwise this only
  1251 + // affects this particular query type - then again maybe that's what we want...
  1252 + for ($i = 0; $i < $this->used_tables['document_fields_link']; ++$i)
  1253 + {
  1254 + if ($i > 0) $counter = $i;
  1255 + else $counter = '';
  1256 +
  1257 + $sql .= ' LEFT JOIN document_fields_link pdfl' . $counter . ' '
  1258 + . 'ON dmv.id=pdfl' . $counter . '.metadata_version_id ' . "\n";
  1259 + }
1248 1260 }
1249 1261  
1250 1262 if ($this->used_tables['tag_words'] > 0)
... ... @@ -1374,7 +1386,6 @@ class SQLQueryBuilder implements QueryBuilder
1374 1386  
1375 1387 public function buildComplexQuery($expr)
1376 1388 {
1377   -// print "building complex \n\n";
1378 1389 $this->exploreExprs($expr);
1379 1390  
1380 1391 $sql = $this->buildCoreSQL();
... ... @@ -1396,7 +1407,6 @@ class SQLQueryBuilder implements QueryBuilder
1396 1407  
1397 1408 public function buildSimpleQuery($op, $group)
1398 1409 {
1399   -// print "building simple \n\n";
1400 1410 $this->exploreGroup($group);
1401 1411  
1402 1412 $sql = $this->buildCoreSQL();
... ...
search2/search/fields/AnyMetadataField.inc.php
... ... @@ -40,7 +40,7 @@
40 40 class AnyMetadataField extends DBFieldExpr
41 41 {
42 42 public $general_op = ExprOp::CONTAINS;
43   -
  43 + public $references = 0;
44 44  
45 45 public function __construct()
46 46 {
... ... @@ -48,6 +48,34 @@ class AnyMetadataField extends DBFieldExpr
48 48 $this->setAlias('Metadata');
49 49 }
50 50  
  51 + /*
  52 + * Overridden function to adjust table alias in cases of
  53 + * the document_fields_link table being included more than once
  54 + *
  55 + * NOTE this only works in conjunction with code in expr.inc.php which adds the left joins to the db query.
  56 + * I don't like this and think we should look for a way to make the table joining more generic
  57 + * such that it can be controlled via these classes and thereby contained as a unit.
  58 + */
  59 + public function modifyName($name)
  60 + {
  61 + if ($this->references > 0)
  62 + {
  63 + static $count = 0;
  64 + if ($count >= $this->references)
  65 + {
  66 + $count = 0;
  67 + }
  68 +
  69 + if ((($pos = strpos($name, '.')) !== false) && ($count != 0))
  70 + {
  71 + $name = substr($name, 0, $pos) . $count . substr($name, $pos);
  72 + }
  73 + ++$count;
  74 + }
  75 +
  76 + return $name;
  77 + }
  78 +
51 79 public function getInputRequirements()
52 80 {
53 81 return array('value'=>array('type'=>FieldInputType::TEXT));
... ...