Search code examples
phppostgresqlzend-db

SQLSTATE[42601]: Syntax error: 7 Inner Join error


I have a problem with a line on my SQL/Zend query, but I didn't understand about this.

public function getByDemande($idDemande, $typeTiers) {

        $select = $this -> newSelect(true);
        $select -> from(array('d' => 'Demande'));
        $select -> join(array('s' => 'Suivi'), 'd."idDemande" = s."idDemande"');
        $select -> join(array('t' => $typeTiers),'d."idContact" = t."idTiers"');
        $select -> joinLeft(array('tet' => 'TelephoneTiers'), 't."idTiers" = tet."idTiers"');
        $select -> joinLeft(array('mt' => 'MailTiers'), 't."idTiers" = mt."idTiers"');
        $select -> join(array('at' => 'AdresseTiers'), 't."idTiers" = at."idTiers"');
        $select -> where('d."idDemande" = s."idDemande"', $idDemande);
        $select -> where('d.actif is true');
        $select -> order('s.dateHeure DESC');

        $resultat = $select->__toString();
        echo "$resultat\n";

        return $this->fetchAll($select);
    }

I launch my SQL/Zend query, but this throws an error (SQLSTATE[42601]: Syntax error: 7 ERREUR: identifiant délimité de longueur nulle sur ou près de « "" »↵LINE 3: INNER JOIN "" AS "t" ON d."idContact" = t."idTiers")

On the other file, I coded to recover data from this SQL query.

$data = $this->_getPostData();
        $idDemande = $data['idDemande'];
        $idTypeTiers = $data['idTypeTiers'];
        $typeTiers = null;

        $gtwDemande = new LogisCom_Model_Gateway_Demande();
        $resultatDemande = $gtwDemande->getByDemande($idDemande, $typeTiers);
        $resultatDemande = $resultatDemande->toArray();

        switch ($idTypeTiers) {

            case TypeTiers::BENEFICIAIRE :
                $typeTiers = "Beneficiaire";
                break;
            case TypeTiers::SALARIE :
                $typeTiers = "Salarie";
                break;
            case TypeTiers::PROSPECT :
                $typeTiers = "Prospect";
                break;
            case TypeTiers::CANDIDAT :
                $typeTiers = "Candidat";
                break;
            case TypeTiers::ENTOURAGE :
                $typeTiers = "Entourage";
                break;
            case TypeTiers::FINANCEUR :
                $typeTiers = "Financeur";
                break;
            case TypeTiers::AUTRE :
                $typeTiers = "StructureExterne";
                break;
        }

Thanks for help.


Solution

  • It seems the variable $typeTiers is either not defined or an empty string. Please try to place a var_dump($typeTiers) at the beginning of your method to see what the variable in question contains. If it is empty, just don't call your method with an emtpy string by just checking beforehand.

    In your switch/case you don't have a default fallback. So it is possible that your $idTypeTiers has a value not in your cases, and $typeTiers keeps being null. But your call to getByDemande is done even before this switch, so even with the fallback your variable would always be null.

    Addendum:

    You might want to translate the error messages to English, because not everyone speaks french (this also applies to other languages). This way, it will be easier for people to understand your error message without the use of a translator etc.