createCommand 未定义的 js
createCommand undefined js
我到处都找遍了,找不到适合我的答案我已经创建了命令并且它工作正常但是我得到了未定义
public function actionTotal($id)
{
$query1 = new Query;
$query1 ->select('sum(price) price')
->from('patient_services')
->where('patient_id=:id', array(':id'=>$id));
$command1 = $query1->createCommand();
$price = $command1->queryAll();
echo Json::encode($price);
}
我的jQuery代码:
$('#receipts-patient_id').change(function()
{
var service_id =$(this).val();
$.get('index.php?r=receipts/total',{ id : service_id},function(data)
{
var data=$.parseJSON(data);
$('#receipts-price').attr('value',data.price); // here i got nothing
alert(data.price); // here i got undefined
});
});
您需要 queryScalar 来获取价格
$price = $command1->queryScalar();
queryAll() return 所有模型.. 查询标量仅第一行值的第一列
尝试参考数据[0]
$('#receipts-price').attr('value',data[0].price)
我到处都找遍了,找不到适合我的答案我已经创建了命令并且它工作正常但是我得到了未定义
public function actionTotal($id)
{
$query1 = new Query;
$query1 ->select('sum(price) price')
->from('patient_services')
->where('patient_id=:id', array(':id'=>$id));
$command1 = $query1->createCommand();
$price = $command1->queryAll();
echo Json::encode($price);
}
我的jQuery代码:
$('#receipts-patient_id').change(function()
{
var service_id =$(this).val();
$.get('index.php?r=receipts/total',{ id : service_id},function(data)
{
var data=$.parseJSON(data);
$('#receipts-price').attr('value',data.price); // here i got nothing
alert(data.price); // here i got undefined
});
});
您需要 queryScalar 来获取价格
$price = $command1->queryScalar();
queryAll() return 所有模型.. 查询标量仅第一行值的第一列
尝试参考数据[0]
$('#receipts-price').attr('value',data[0].price)