Zum Inhalt springen

Doppeltes Join - Unknown Column

Erstellt am 13. Februar 2013 · 8 Antworten · letzte Antwort am 14. Februar 2013

Tags: Frage

fuzz ·

Hallo,

muss in meinem Repository an zwei stellen schauen ob ein Institut aus einem bestimmten Land kommt. Das habe ich wie folgt versucht:

$query->logicalOr(							$query->equals('coordinatingInstitute.country.uid',(integer)$sterm['country']),
	$query->equals('institutes.country.uid',(integer)$sterm['country'])
)

Das führt jedoch zu einer Exception mit folgender Meldung:

#1247602160: Unknown column 'tx_nnctcp_project_institute_mm.uid_foreign' in 'on clause': SELECT DISTINCT tx_nnctcp_domain_model_project.* FRO...

Lasse ich eine "Equals" Abfrage von beiden weg, funktioniert alles wunderbar.

Mache ich was falsch oder wie kann ich das lösen?

Unbekannter Benutzer ·

Du hast nicht die ganze Query gepostet und ich kenne dein TCA / Models und Tables nicht, aber 'tx_nnctcp_project_institute_mm.uid_foreign' dürfte ein Feld in einer Standard TYPO3 MM Tabelle sein. D.h. du hast wahrscheinlich im TCA eine MM Tabelle konfiguriert (tx_nnctcp_project_institute_mm). Existiert diese? Wenn ja sollte sie auch ein uid_foreign Feld haben...

fuzz ·

Hallo,

danke erstmal für die fixe antwort...

ja die Tabelle existiert und wenn ich nur ein Equals hinzufüge (anstatt wie im beispiel zwei) geht es. Es geht nicht wenn 2 unterschiedliche Felder (Institutes und CoordinatingInstitute) auf die selbe M:N Tabelle zugreifen wollen, dann macht extbase zweimal einen JOIN auf ein und die selbe Tabelle womit mysql wohl nicht klar kommt.

hier die ganze query / fehlermeldung...

#1247602160: Unknown column 'tx_nnctcp_project_institute_mm.uid_foreign' in 'on clause': 
SELECT DISTINCT tx_nnctcp_domain_model_project.* FROM tx_nnctcp_domain_model_project LEFT JOIN tx_nnctcp_domain_model_institute ON tx_nnctcp_project_institute_mm.uid_foreign=tx_nnctcp_domain_model_institute.uid LEFT JOIN tx_nnctcp_domain_model_country ON tx_nnctcp_domain_model_institute.country=tx_nnctcp_domain_model_country.uid LEFT JOIN tx_nnctcp_project_institute_mm ON tx_nnctcp_domain_model_project.uid=tx_nnctcp_project_institute_mm.uid_local WHERE ((tx_nnctcp_domain_model_project.pid IN ('73') AND tx_nnctcp_domain_model_project.refid LIKE '%1225%') AND (tx_nnctcp_domain_model_country.uid = '5' OR tx_nnctcp_domain_model_country.uid = '5')) AND tx_nnctcp_domain_model_project.deleted=0 AND tx_nnctcp_domain_model_project.t3ver_state<=0 AND tx_nnctcp_domain_model_project.pid<>-1 AND tx_nnctcp_domain_model_project.hidden=0 AND tx_nnctcp_domain_model_project.starttime<=1360766220 AND (tx_nnctcp_domain_model_project.endtime=0 OR tx_nnctcp_domain_model_project.endtime>1360766220) AND tx_nnctcp_domain_model_project.sys_language_uid IN (0,-1) AND tx_nnctcp_domain_model_project.pid IN (73, 77, 78-) AND tx_nnctcp_domain_model_institute.deleted=0 AND tx_nnctcp_domain_model_institute.t3ver_state<=0 AND tx_nnctcp_domain_model_institute.pid<>-1 AND tx_nnctcp_domain_model_institute.hidden=0 AND tx_nnctcp_domain_model_institute.starttime<=1360766220 AND (tx_nnctcp_domain_model_institute.endtime=0 OR tx_nnctcp_domain_model_institute.endtime>1360766220) AND tx_nnctcp_domain_model_institute.sys_language_uid IN (0,-1) AND tx_nnctcp_domain_model_institute.pid IN (73, 77, 78-) AND tx_nnctcp_domain_model_country.deleted=0 AND tx_nnctcp_domain_model_country.t3ver_state<=0 AND tx_nnctcp_domain_model_country.pid<>-1 AND tx_nnctcp_domain_model_country.hidden=0 AND tx_nnctcp_domain_model_country.starttime<=1360766220 AND (tx_nnctcp_domain_model_country.endtime=0 OR tx_nnctcp_domain_model_country.endtime>1360766220) AND tx_nnctcp_domain_model_country.sys_language_uid IN (0,-1) AND tx_nnctcp_domain_model_country.pid IN (73, 77, 78-) ORDER BY tx_nnctcp_domain_model_project.refid ASC (More information)

Tx_Extbase_Persistence_Storage_Exception_SqlError thrown in file
C:\TYPO3\4.7.0\htdocs\Introduction\typo3\sysext\extbase\Classes\Persistence\Storage\Typo3DbBackend.php in line 1020.
Unbekannter Benutzer ·

Ja ich seh schon, das ist dann wohl ein Problem im QueryBuilder von Extbase. Da wird auf die mm Tabelle gematched bevor die überhaupt gejoined wurde...

Probier doch bitte mal testweise diese Query mit dieser Veränderung:

ORIGINAL:

SELECT DISTINCT tx_nnctcp_domain_model_project.* FROM tx_nnctcp_domain_model_project LEFT JOIN tx_nnctcp_domain_model_institute ON tx_nnctcp_project_institute_mm.uid_foreign=tx_nnctcp_domain_model_institute.uid LEFT JOIN tx_nnctcp_domain_model_country ON tx_nnctcp_domain_model_institute.country=tx_nnctcp_domain_model_country.uid LEFT JOIN tx_nnctcp_project_institute_mm ON tx_nnctcp_domain_model_project.uid=tx_nnctcp_project_institute_mm.uid_local WHERE HIER KOMMEN DIE GANZEN WHERE BEDINGUNGEN....

GEÄNDERT:

SELECT DISTINCT tx_nnctcp_domain_model_project.* FROM tx_nnctcp_domain_model_project LEFT JOIN tx_nnctcp_project_institute_mm ON tx_nnctcp_domain_model_project.uid=tx_nnctcp_project_institute_mm.uid_local LEFT JOIN tx_nnctcp_domain_model_institute ON tx_nnctcp_project_institute_mm.uid_foreign=tx_nnctcp_domain_model_institute.uid LEFT JOIN tx_nnctcp_domain_model_country ON tx_nnctcp_domain_model_institute.country=tx_nnctcp_domain_model_country.uid  WHERE HIER KOMMEN DIE GANZEN WHERE BEDINGUNGEN....

Wahrscheinlich funktioniert es dann auch noch nicht, aber sieht zumindest realistischer aus 😉

fuzz ·

Hui, also ein Bug in extbase?
Gibt es dafür irgendwie einen patch oder so?

Also der Query funktioniert, jedoch liefert er 0 datensätze zurück obwohl mindestens 1 erscheinen würde.

Ich werd mir da schon einen query zusammen bauen können der klappt, aber lieber würde ich natürlich die extbase funktionen nutzen anstatt ein eigenes sql statement zu schreiben.

Unbekannter Benutzer ·

Joa, ich würde sagen das sieht nach Extbase Bug aus, da ich den Bug bisher nicht kannte, kann ich auch nichts über eventuelle Patches sagen. Guck am besten mal in review.typo3.org ob da was zu finden ist.

beo6 ·

Ich bin mir nicht ganz sicher.

Aber müsste das nicht mit "$query->matching();" gekapselt werden?

Dann müsste es so aussehen:

<?php
$query = $this->createQuery();

$query->matching(
	$query->logicalOr(
		$query->equals('coordinatingInstitute.country.uid',(integer)$sterm['country']),
		$query->equals('institutes.country.uid',(integer)$sterm['country'])
	)
);

return $query->execute();
fuzz ·

Moin,

also in dem Tracker habe ich nichts passendes gefunden... war mir aber auch nicht 100%ig sicher nach was ich gucken soll.

Jedenfalls habe ich den Query mal etwas aufgebohrt. Mein Thema "Doppeltes Join" ist auch etwas fehl. Die Reihenfolge der Joins ist fehlerhaft. Wenn ich diese umdrehe gibt es zumindest keine Fehler mehr...

Jetzt habe ich aber das Problem das 0 Ergebnisse geliefert werden anstatt 1. Das kommt weil das Feld "coordinatingInstitute" aus der Projects Tabelle nicht berücksichtigt wird.

Was mache ich falsch bzw. kann wie kann ich das lösen? Idealerweise über das Extbase Query Object... Denn vielleicht baue ich da ja schon den Query falsch zusammen der dann den "Bug" verursacht?

Hier mal der Query mit den Joins in der korrekten Reihenfolge (aber 0 ergebnisse) und mein Extbase Query Aufruf (welche den fehlerhaften query) produziert.

SELECT DISTINCT tx_nnctcp_domain_model_project.*

           FROM tx_nnctcp_domain_model_project 

      LEFT JOIN tx_nnctcp_project_institute_mm 
             ON tx_nnctcp_domain_model_project.uid = tx_nnctcp_project_institute_mm.uid_local 

      LEFT JOIN tx_nnctcp_domain_model_institute 
             ON tx_nnctcp_project_institute_mm.uid_foreign = tx_nnctcp_domain_model_institute.uid 

      LEFT JOIN tx_nnctcp_domain_model_country 
             ON tx_nnctcp_domain_model_institute.country = tx_nnctcp_domain_model_country.uid 

          WHERE ( 

                  (
                    tx_nnctcp_domain_model_project.pid IN ('73') 
                    AND 
                    tx_nnctcp_domain_model_project.refid LIKE '%1225%'
                  ) 

                  AND tx_nnctcp_domain_model_country.uid = '5'
                ) 
                
                AND tx_nnctcp_domain_model_project.deleted = 0 
                AND tx_nnctcp_domain_model_project.t3ver_state <= 0 
                AND tx_nnctcp_domain_model_project.pid <> -1 
                AND tx_nnctcp_domain_model_project.hidden = 0 
                AND tx_nnctcp_domain_model_project.starttime <= 1360828500 
                
                AND 

                (
                  tx_nnctcp_domain_model_project.endtime = 0 
                  OR 
                  tx_nnctcp_domain_model_project.endtime > 1360828500
                ) 

                AND tx_nnctcp_domain_model_project.sys_language_uid IN (0,-1) 
                AND tx_nnctcp_domain_model_project.pid IN (73, 77, 78-) 
                AND tx_nnctcp_domain_model_institute.deleted = 0 
                AND tx_nnctcp_domain_model_institute.t3ver_state <= 0 
                AND tx_nnctcp_domain_model_institute.pid <> -1 
                AND tx_nnctcp_domain_model_institute.hidden = 0 
                AND tx_nnctcp_domain_model_institute.starttime <= 1360828500 

                AND 

                (
                  tx_nnctcp_domain_model_institute.endtime = 0 
                  OR 
                  tx_nnctcp_domain_model_institute.endtime>1360828500
                ) 

                AND tx_nnctcp_domain_model_institute.sys_language_uid IN (0,-1) 
                AND tx_nnctcp_domain_model_institute.pid IN (73, 77, 78-) 
                AND tx_nnctcp_domain_model_country.deleted = 0 
                AND tx_nnctcp_domain_model_country.t3ver_state <= 0 
                AND tx_nnctcp_domain_model_country.pid <> -1 
                AND tx_nnctcp_domain_model_country.hidden = 0 
                AND tx_nnctcp_domain_model_country.starttime <= 1360828500 
                
                AND 

                (
                  tx_nnctcp_domain_model_country.endtime = 0 
                  OR 
                  tx_nnctcp_domain_model_country.endtime > 1360828500
                ) 

                AND tx_nnctcp_domain_model_country.sys_language_uid IN (0,-1) 
                AND tx_nnctcp_domain_model_country.pid IN (73, 77, 78-) 

       ORDER BY tx_nnctcp_domain_model_project.refid ASC
public function findBySearch(array $sterm,$sorting = "title",$order = "ASC") {
		// Ggf. überschreibt der Benutzer Query
		if ( ($userQuery = $this->getUserQuery(__FUNCTION__)) != false ) return $this->executeUserQuery($userQuery);
		
		// Query vorbereiten
		$query      = $this->createQuery();
		$logicalAnd = array($query->in('pid',$this->getPidList(false,__FUNCTION__)));
		
		// Suchkriterien bilden
		if ( $sterm['title'] != '' )        array_push($logicalAnd, $query->like('title','%'.$sterm['title'].'%'));
		if ( $sterm['refid'] > 0 )          array_push($logicalAnd, $query->like('refid','%'.(integer)$sterm['refid'].'%'));
		if ( $sterm['subject'] > 0 )        array_push($logicalAnd, $query->equals('subjects.uid',(integer)$sterm['subject']));
		if ( $sterm['collaboration'] > -1 ) array_push($logicalAnd, $query->equals('collaboration_type',(integer)$sterm['collaboration']));
		if ( $sterm['institute'] > 0 )      array_push($logicalAnd, $query->equals('institutes.uid',(integer)$sterm['institute']));
		if ( $sterm['status'] > -1 )        array_push($logicalAnd, $query->equals('status',(integer)$sterm['status']));
		if ( $sterm['country'] > 0 )        array_push($logicalAnd, $query->logicalOr(
												# DEBUG: Dies führt zu einem doppelten Join und damit SQL Fehler, Lösung???
												$query->equals('coordinatingInstitute.country.uid',(integer)$sterm['country']),
												$query->equals('institutes.country.uid',(integer)$sterm['country'])
											));
		
		// Sortierung festlegen
		$order = ( $order === 'ASC' ) ? Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING : Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING;
		
		// Ausführen und Ergebnis
		return $query->matching($query->logicalAnd($logicalAnd))->execute();
	}