You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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() returns null (since the object hasn't been saved to the database yet, so no ID exists). This leads to broken SQL syntax like =evaluation_symptom.ID in 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

  1. 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.
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:27:31