-1

私のエンティティには正しい注釈などがあるように見えるため、ここで何が問題なのかを理解できないようです。SQLをNavicatで直接実行するように変換すると機能します。

具体的なエラーは

[Semantical Error] line 0, col 120 near 'city_id = 2)': Error: Class Simpleweb\Entity\Product has no field or association named city_id 

city_id に関連する製品エンティティ セクション (City は OneToMany 関連エンティティ)

/**
 * @ManyToOne(targetEntity="City", inversedBy="products")
 * @JoinColumn(name="city_id", referencedColumnName="id")
 **/
private $city;

コントローラーのコードは、ユーザーが選択したフィルターの数に基づいて動的クエリを作成しようとしています。これに関連するものは、where 句に都市 ID 番号を追加しようとします。

public function findByPhrases($phrase, $sortBy = null, $cityFilter = null){
    $searchWords = $this->phraseToWords($phrase);
    $result = null;

    if(!is_null($searchWords)){
        /*
         * Build query, e.g. below:
         *
         * SELECT * FROM products
         * WHERE description_short
         * (LIKE '%sander%' OR name LIKE '%sander%') AND
         * (city_id = 1 OR city_id = 3)
         */
        try{
            $sql = "SELECT p FROM Simpleweb\Entity\Product p WHERE ";

            // Append an OR where clause onto the end for each word
            $sql .= "(";
            foreach($searchWords as $word){
                $sql .= "(p.description_short LIKE '%$word%') OR (p.name LIKE '%$word%') OR ";
            }

            $sql = substr($sql, 0, -4); // Remove the last 'OR' from the end of the SQL statement
            $sql .= ")";

            if($cityFilter != null){
                $sql .= " AND (";
                 foreach($cityFilter as $city){
                     $sql .= "p.city_id = $city OR ";
                 }
                $sql = substr($sql, 0, -4);
                $sql .= ")";
            }

            // Add sorting statement
            if(strlen($sortBy) >= 1){
                switch($sortBy){
                    case 'dateadded':
                        $sql .= " ORDER BY p.created ASC";
                        break;
                    case 'priceasc':
                        $sql .= " ORDER BY p.totalprice ASC";
                        break;
                    default:
                        break;
                }
            }

            echo($sql);

            $em = $this->getEntityManager();
            $qb = $em->createQuery($sql);

            $result = $qb->getResult();
        }catch(Exception $ex){
            echo($ex->getMessage());
        }
        return $result;
    }else{
        return null;
    }
}
4

1 に答える 1

0

わかりました、それを整理しました:

$sql = "SELECT p FROM Simpleweb\Entity\Product p WHERE ";

に変更されました

$sql = "SELECT p FROM Simpleweb\Entity\Product p JOIN p.city c WHERE ";

$sql .= "p.city_id = $city OR ";

に変更されました

$sql .= "c.id = $city OR ";
于 2013-02-13T10:52:46.590 に答える