Naeem Marzu
Naeem Marzu

Reputation: 186

createCommand undefined js

I've looked everywhere and couldn't find an answer for me i've create command and its works fine but i get undefined

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);

}

my jQuery Code :

 $('#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 
    });
  });

inspect with Firebug

Upvotes: 0

Views: 63

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133370

You need queryScalar for obtain the price only

$price = $command1->queryScalar();  

queryAll() return all the models .. query scalar the first column of the firts row value only

try referring to data[0]

$('#receipts-price').attr('value',data[0].price)

Upvotes: 1

Related Questions