如何在cakephp中按日期排序数组

igsr9ssn  于 2022-11-11  发布在  PHP
关注(0)|答案(2)|浏览(154)

我有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
            )

    )

  )

先谢谢你。

e4yzc0pl

e4yzc0pl1#

**选项#1:**建立第三个名为“BookingAndMessage”的模型。您可以使用模型的afterSave方法(在Booking和Message上),在新模型中建立重复的记录。然后,您可以按适当的排序顺序查询BookingAndMessage。
选项2:要解决这个问题,您需要使用PHP的usort函数(这里也解释了PHP Sort a multidimensional array by element containing date)。

<?php

$BookingsAndMessages = array_merge($bookings, $messages);

function date_compare($a, $b)
{   
   $modelKeyA = array_key_exists('Booking',$a) ? 'Booking' : 'MessageDetail';
   $modelKeyB = array_key_exists('Booking',$b) ? 'Booking' : 'MessageDetail';
   $t1 = strtotime($a[$modelKeyA]['created']);
   $t2 = strtotime($b[$modelKeyB]['created']);
   return $t1 - $t2;
}

usort($BookingsAndMessages, 'date_compare');
// Would sort the merged records by `created` date
?>

选项2的缺点是,您必须为每个字段(和排序方向)创建规则。

sgtfey8w

sgtfey8w2#

也许你应该先做你的查询。

$bookings = $this->Bookings->find('all', [
        'conditions' => $conditions,'limit' =>10,
]);

$messages = $this->find('all', [
        'conditions' => $conditions,'limit' =>10
]);

$data = $bookings->union($messages);
// This works if your first selected field is the one you want to sort. 
$data->epilog('ORDER BY 1 ASC');

如何在CakePHP 3中修改UNION查询?

相关问题