【问题标题】:how to sort array date wise in cakephp如何在cakephp中对数组日期进行明智的排序
【发布时间】:2013-04-25 12:37:36
【问题描述】:

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

    )

  )

提前致谢。

【问题讨论】:

  • 以上代码只对消息进行排序。不预订
  • 请澄清 - 很难确定要问什么。
  • @YogeshSaroya 您在预订和消息之间有关联吗?听起来,这可能是您希望数据库为您做的事情,而不是在 PHP 中手动执行?
  • @SamDelaney 在之前的评论中所说的可能是要走的路。如果您发布原始查询以检索数据和模型之间的关系,那么有人可能会为您提供更多帮助。在问题中添加 sql 标签也可能有助于在这里吸引一些 sql 专家的注意。

标签: arrays cakephp cakephp-2.0


【解决方案1】:

选项 #1: 创建名为“BookingAndMessage”的第三个模型。您可以使用模型的afterSave 方法(在预订和消息上)在新模型中创建重复记录。然后,您可以按正确的排序顺序查询 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 的缺点是,您必须为每个字段(和排序方向)创建规则。

【讨论】:

    【解决方案2】:

    也许您应该首先Union 查询,Order

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

    参考: How do you modify a UNION query in CakePHP 3?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 2023-03-04
      • 2010-10-10
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多