フォームからAJAX呼び出しを実行しようとしていますが、モデルにコントローラーメソッドを記述して、結果をフォームに返しています。Yii SQLステートメント、またはYiiコンテキストでのデータの取得/受け渡しは初めてです。誰かがこれを書き込もうとして私の論理を導き/修正できますか?
これは元のmysqlクエリです
"SELECT AVG(rate_syr_mh)FROM packaging_metrics WHERE country LIKE '" . mysql_real_escape_string($country)."'". "AND std_rate != 0
「これはこれまでの私のコントローラーアクションです。ここで正しい方向に進んでいるかどうかはわかりません。パラメーター$countryは、特定の変更でフォームから送信されたフォーム入力である必要があります。
public function countryRate($country)
{
$country_std_rate = Yii::app()->db->createCommand()
->select('AVG(std_rate)')
->from('packaging_metrics')
->where(array('like', 'country', '%$country%'))
->queryRow();
return $country_std_rate;
}
エントリがゼロにならないように、元のクエリの一部を追加するにはどうすればよいですか?
また、この方法でクエリを返すことで、AJAXによる取得で数値の結果を受け取り、別のフォームフィールドに入力できるようになりますか?