0

How to change the following sql query to yii2.

SELECT 
    a.id, a.name_of_flight, a.time_of_flight, (no_of_passenger-b.cnt) as avail 
FROM 
    flight_schedule a 
LEFT JOIN 
    (SELECT flight_time, COUNT(id) AS cnt FROM book_eticket WHERE flight_date='2016-06-01' GROUP BY flight_time) b 
ON a.id = b.flight_time
clash
  • 5
  • 3

1 Answers1

0

I'm not sure but you can use this logic. For more detail pls visit official page.

flight_schedule::find()
    ->select(['id', 'name_of_flight', ' time_of_flight'])
    ->distinct()
    ->joinWith('bookEticket')
    ->all();

And add relation to flight_schedule model:

public function getBookEticket()
{
    $this->hasOne(book_eticket::className(), ['id' => 'flight_time']);
}
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130