Yii2 join a subquery

I'm building a booking site, where properties (ex: hotel) have rooms and you can book them.

I've made a raw SQL query which filters the available rooms in a specified date range, but I don't know how to implement it in a YII AR way: using with active record find() as a relation

Do you guys have any suggestion or opinion about this?

SELECT
  property.id,
  property_lang.name,
  max_people.total
FROM property
  JOIN property_lang ON (property.id = property_lang.property_id AND property_lang.lang_id = 'hu')
  JOIN (
         SELECT
           property_id     AS id,
           sum(max_people) AS total
         FROM room
         WHERE room.id NOT IN (
           SELECT room_id
           FROM booking
             JOIN booking_status ON (booking.last_status_id = booking_status.id)
             JOIN booking_room ON (booking.id = booking_room.id)
             JOIN property ON booking.property_id = property.id
           WHERE (booking_status.status > -1 OR booking_status.status IS NULL)
                 AND booking.check_in < '2017-10-18'
                 AND booking.check_out > '2017-10-14'
         )
         GROUP BY property_id
       ) max_people
    ON max_people.id = property_id

ORDER BY property.id

Upvotes: 2

Views: 2593

Answers (1)

MasteRus
MasteRus

Reputation: 111

The simplest solution is use the createCommand with params:

\Yii::$app->db->createCommand("SELECT
property.id,
  property_lang.name,
  max_people.total
FROM property
  JOIN property_lang ON (property.id = property_lang.property_id AND property_lang.lang_id = 'hu')
  JOIN (
         SELECT
           property_id     AS id,
           sum(max_people) AS total
         FROM room
         WHERE room.id NOT IN (
           SELECT room_id
           FROM booking
             JOIN booking_status ON (booking.last_status_id = booking_status.id)
         JOIN booking_room ON (booking.id = booking_room.id)
         JOIN property ON booking.property_id = property.id
       WHERE (booking_status.status > -1 OR booking_status.status IS NULL)
             AND booking.check_in < ':startdate'
             AND booking.check_out > ':finishdate'
     )
     GROUP BY property_id
   ) max_people
ON max_people.id = property_id

ORDER BY property.id")
             ->bindValues([':startdate' => $startDate, ':finishdate' => $startDate])
             ->execute();

Other way is to split query for subqueries and try to use AR. It will be something like that:

$subSubQuery=Booking::find()
->select(['room_id'])
->join('booking_status', 'booking.last_status_id = booking_status.id')
->join('booking_room', 'booking.id = booking_status.id')
->join('property', 'booking.property_id= booking_propery.id')
->andWhere(['or',
        ['>','booking_status.status',-1],
        [ 'booking_status.status'=>null],
])
->andWhere(['<','booking.check_in',$startDate])
->andWhere(['>','booking.checkout',$finishDate])
->groupBy('property_id');

$subQuery = Room::find()->select(['property_id as id','sum(max_people) as total'])->andFilterWhere(['not in', 'answers_metering.id', $subSubQuery]);

$query = Property::find()->select(['property.id','property_lang.name','max_people.total'])
->join($subQuery. ' as max_people', 'max_people.id = property.id')
->join('property_lang', 'property.id = property_lang.property_id AND property_lang.lang_id = \'hu\'')
->orderBy(['property.id' => SORT_ASC])
->all();

Upvotes: 2

Related Questions