micky micky - 4 months ago 9
Ajax Question

using ajax jquery with ojects

$sql="SELECT * FROM seat WHERE type=$type AND seat=$seat";
$getseats=Yii::$app->db->createCommand($sql)->queryAll();
echo json_encode(array('status' => TRUE, 'getseats'=>$getseats)); die;


this gives $getseats values in array.so i could do this:

success: function(response){
var res=$.parseJSON(response);
if(res.status == true)
{
var seat='';
for(var i=0; i<res.getseats.length; i++)
{
seat += '<option value='+res.getseats[i].seat_id+'>'+res.getseats[i].seat+'</option>';
$('#seats').html(seat);
}
}


but what if i use active record?

$getseats=Seat::where(['type'=$type])->where(['seat'=>$seat])->all();
echo json_encode(array('status' => TRUE, 'getseats'=>$getseats)); die;

Answer

For activeRecord you should use (this return models)

$getseats=Seat::Find()->where(['type'=$type, 'seat'=>$seat])->all();   
echo json_encode(array('status' => TRUE,  'getseats'=>$getseats));
die;

if you want the result like an array you can use

$getseats=Seat::Find()->where(['type'=$type, 'seat'=>$seat])->asArray()->all();