我有两个模型,列表和预订。列表有很多预订。我正在尝试编写一个查询以返回在特定时间段内没有预订的所有列表。
我到目前为止,但我仍然是 cakephp 的新手,我的 sql 也不是很好。在我的控制器方法中,我有
$conditions = array(
"OR" => array(
"Listing.address_one LIKE" => "%".$area."%",
"Listing.address_two LIKE" => "%".$area."%",
"Listing.city LIKE" => "%".$area."%",
"Listing.postcode LIKE" => "%".$area."%",
"Listing.title LIKE" => "%".$area."%",
)
);
$this->paginate = array(
'conditions'=> $conditions,
'joins'=>array(
array(
'table'=>'bookings',
'alias'=>'Booking',
'type' =>'inner',
'conditions' =>array('Booking.listing_id = Listing.id', 'Booking.checkin NOT BETWEEN ? AND ?' => array($to, $from))
)
),
'limit' => 10,
);
$data = $this->paginate();
这只会获取所有实际具有与之关联的预订的列表。不仅如此,它还不会忽略在给定时间段内预订的任何预订。
它创建的 SQL 是:
SELECT
...
FROM
`database`.`listings` AS `Listing`
INNER JOIN
`database`.`bookings` AS `Booking` ON (
`Booking`.`listing_id` = `Listing`.`id` AND
`Booking`.`checkin` NOT BETWEEN '18-07-2013' AND '10-07-2013'
)
LEFT JOIN
`database`.`users` AS `User` ON (
`Listing`.`user_id` = `User`.`id`
)
WHERE (
(`Listing`.`address_one` LIKE '%belfast%') OR
(`Listing`.`address_two` LIKE '%belfast%') OR
(`Listing`.`city` LIKE '%belfast%') OR
(`Listing`.`postcode` LIKE '%belfast%') OR
(`Listing`.`title` LIKE '%belfast%')
)
LIMIT 10
这看起来有点正确(?)但显然不是。有人有什么想法吗?谢谢