Yii2加入子查询

时间:2017-09-29 13:08:00

标签: php mysql yii yii2

我正在建立一个预订网站,其中的房产(例如:酒店)有房间,您可以预订。

我做了一个原始SQL查询,它过滤了指定日期范围内的可用房间,但我不知道如何以YII AR方式实现它:使用活动记录find()作为关系

你们对此有什么建议或意见吗?

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

1 个答案:

答案 0 :(得分:2)

最简单的解决方案是使用带有参数的createCommand:

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

其他方法是拆分子查询的查询并尝试使用AR。它会是这样的:

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