Symfony新建症状触发SQL 1064语法错误,求排查方案
Fixing the 500 Error & PropelException: SQL Syntax Error in evaluation_symptom Module
The Root Cause
Your critical mistake is in the line $criteria->addJoin(self::ID, $id); inside getInferredRemediesBySymptomId:
addJoin()is only for defining table relationships (like joining on foreign keys), not for adding WHERE clause conditions. It expects two column references, not a column and a raw value.- When creating a new symptom,
$this->object->getPrimaryKey()returnsnull(since the object hasn't been saved to the database yet, so no ID exists). This leads to broken SQL syntax like=evaluation_symptom.IDin your error message—MySQL has no idea what to do with that random equals sign.
The Fix
Replace the incorrect addJoin call with a proper WHERE condition, and add a guard clause to handle the "new symptom" case where ID is empty:
public static function getInferredRemediesBySymptomId($id) { // Guard clause: return empty array if no valid symptom ID is provided if (empty($id)) { return []; } $criteria = new Criteria(); $criteria->addSelectColumn("evaluation_remedy.ID"); $criteria->addSelectColumn("evaluation_remedy.TITLE"); $criteria->addJoin(self::ID, EvaluationSymptomCausePeer::EVALUATION_SYMPTOM_ID, Criteria::LEFT_JOIN); $criteria->addJoin(EvaluationSymptomCausePeer::EVALUATION_CAUSE_ID, EvaluationCausePeer::ID, Criteria::LEFT_JOIN); $criteria->addJoin(EvaluationCausePeer::ID, EvaluationCauseRemedyPeer::EVALUATION_CAUSE_ID, Criteria::LEFT_JOIN); $criteria->addJoin(EvaluationCauseRemedyPeer::EVALUATION_REMEDY_ID, EvaluationRemedyPeer::ID, Criteria::LEFT_JOIN); // Replace the wrong addJoin with a proper WHERE condition $criteria->add(self::ID, $id); $exCriteria = $criteria->getNewCriterion(self::IS_ACTIVE, 1); $exCriteria->addOr($criteria->getNewCriterion(EvaluationCausePeer::IS_ACTIVE, 1)); $criteria->add($exCriteria); $criteria->addAscendingOrderByColumn(self::ORD)->addAscendingOrderByColumn(EvaluationCausePeer::ORD); $records = self::doSelectStmt($criteria)->fetchAll(PDO::FETCH_ASSOC); $data = array(); foreach ($records as $key => $record) { $data[$record['ID']] = $record['TITLE']; } return $data; }
Key Notes
- The guard clause (
if (empty($id))) prevents unnecessary (and broken) SQL queries when creating new symptoms—since there's no ID yet, there can't be any inferred remedies to fetch. - Using
$criteria->add(self::ID, $id)generates valid SQL:WHERE evaluation_symptom.ID = ?with parameter binding for$id, fixing the syntax error entirely.
内容的提问来源于stack exchange,提问作者DojoDev
相关产品推荐
相关产品推荐

