[TYPO3-Solr] n:1 relations

Jernej Zorec jernejz at gmail.com
Wed Aug 29 11:17:28 CEST 2012


Hi!

I was searching how to setup the n:1 relation with the SOLR_RELATION 
typoscript and it seems that it is not yet supported according to the 
source code of tx_solr_contentobject_Relation class.

Attached is a patch that implements this feature. Only few lines in the 
getRelatedItemsFromForeignTable method. Tell me what you think and if it 
is possible to include this in the next release.
You would then use TS like this:
registerdovoljenje_stringM = SOLR_RELATION
registerdovoljenje_stringM {
   localField = register_dovoljenj
   foreignField = dovoljenje
   multiValue = 1
}
The additional foreignField property tells that there is a n:1 relation 
instead of the 1:n.

Or tell me that this is already implemented and I that didn't know how 
to configure it properly :)

Regards,
Jernej
-------------- next part --------------
--- class.tx_solr_contentobject_relation.php.orig	2012-08-09 12:12:27.000000000 +0200
+++ class.tx_solr_contentobject_relation.php	2012-08-29 10:44:43.000000000 +0200
@@ -103,27 +103,29 @@
 		$localTableTca  = $GLOBALS['TCA'][$localTableName];
 
 		$localFieldName = $this->configuration['localField'];
+		$foreignFieldName = $this->configuration['foreignField'];
 		$localFieldTca  = $localTableTca['columns'][$localFieldName];
 
 		if (isset($localFieldTca['config']['MM']) && trim($localFieldTca['config']['MM']) !== '') {
 			$relatedItems = $this->getRelatedItemsFromMMTable($localTableName, $localRecordUid, $localFieldTca);
 		} else {
-			$relatedItems = $this->getRelatedItemsFromForeignTable($localFieldName, $localRecordUid, $localFieldTca, $parentContentObject);
+			$relatedItems = $this->getRelatedItemsFromForeignTable($localFieldName, $localRecordUid, $localFieldTca, $parentContentObject, $foreignFieldName);
 		}
 
 		return $relatedItems;
 	}
 
 	/**
-	 * Gets the related items from a table using a 1:n relation.
+	 * Gets the related items from a table using a 1:n or n:1 relation.
 	 *
 	 * @param string $localFieldName Local table field name
 	 * @param integer $localRecordUid Local record uid
 	 * @param array $localFieldTca The local table's TCA
 	 * @param tslib_cObj $parentContentObject parent content object
+ 	 * @param string $foreignFieldName Foreign table field name for n:1 relation
 	 * @return array Array of related items, values already resolved from related records
 	 */
-	protected function getRelatedItemsFromForeignTable($localFieldName, $localRecordUid, array $localFieldTca, tslib_cObj $parentContentObject) {
+	protected function getRelatedItemsFromForeignTable($localFieldName, $localRecordUid, array $localFieldTca, tslib_cObj $parentContentObject, $foreignFieldName) {
 		$relatedItems = array();
 
 		$foreignTableName = $localFieldTca['config']['foreign_table'];
@@ -139,11 +141,15 @@
 			$whereClause = $foreignTableName . '.' . $foreignTableField . ' = ' . (int) $localRecordUid;
 		} else {
 			$foreignTableUids = t3lib_div::intExplode(',', $parentContentObject->data[$localFieldName]);
-
-			if (count($foreignTableUids) > 1) {
-				$whereClause = $foreignTableName . '.uid IN (' . implode(',', $foreignTableUids) . ')';
+			
+			if ( isset($foreignFieldName) ) {
+				$whereClause = $foreignTableName . '.'.$foreignFieldName.' = ' . (int) $localRecordUid;
 			} else {
-				$whereClause = $foreignTableName . '.uid = ' . (int) array_shift($foreignTableUids);
+				if (count($foreignTableUids) > 1) {
+					$whereClause = $foreignTableName . '.uid IN (' . implode(',', $foreignTableUids) . ')';
+				} else {
+					$whereClause = $foreignTableName . '.uid = ' . (int) array_shift($foreignTableUids);
+				}
 			}
 		}
 


More information about the TYPO3-project-solr mailing list