我有2桌的预订和消息,现在我想在收件箱中显示预订请求和消息一次。
$this->paginate = array(
'conditions' => $conditions,'limit' =>10,
'order'=>array('Booking.created'=>'DESC'));
$bookings = $this->paginate('Booking');
$this->paginate = array(
'conditions' => $conditions,'limit' =>10,
'order'=>array('MessageDetail.created'=>'DESC'));
$messages = $this->paginate('MessageDetail');
我已经合并了两个表数据(array_merge($bookings,$messages);)现在我想它排序日期明智(或任何条件)
Array
(
[0] => Array
(
[Booking] => Array
(
[id] => 4
[host_id] => 21
[place_id] => 10
[room_id] => 13
[user_id] => 12
[message_detail_id] => 16
[created] => 2013-04-23 14:44:03
[accept_date] =>
[cancel_date] =>
)
)
[1] => Array
(
[Booking] => Array
(
[id] => 3
[host_id] => 21
[place_id] => 10
[room_id] => 13
[user_id] => 12
[message_detail_id] => 13
[created] => 2013-04-15 14:10:59
[accept_date] => 2013-04-15 14:40:47
[cancel_date] =>
)
)
[2] => Array
(
[MessageDetail] => Array
(
[id] => 17
[message_id] => 2
[user_id] => 12
[sender_id] => 21
[unread] => 0
[created] => 2013-04-24 12:11:47
)
)
[3] => Array
(
[MessageDetail] => Array
(
[id] => 15
[message_id] => 2
[user_id] => 12
[sender_id] => 21
[booking_id] => 3
[unread] => 0
[created] => 2013-04-15 15:01:12
)
)
)
先谢谢你。
2条答案
按热度按时间e4yzc0pl1#
**选项#1:**建立第三个名为“BookingAndMessage”的模型。您可以使用模型的afterSave方法(在Booking和Message上),在新模型中建立重复的记录。然后,您可以按适当的排序顺序查询BookingAndMessage。
选项2:要解决这个问题,您需要使用PHP的usort函数(这里也解释了PHP Sort a multidimensional array by element containing date)。
选项2的缺点是,您必须为每个字段(和排序方向)创建规则。
sgtfey8w2#
也许你应该先做你的查询。
如何在CakePHP 3中修改UNION查询?