如何在 yii2 中对模型数组进行分组

How to group models array in yii2

大家好,我有一个页面,我正在使用来自 modelsforeach 循环显示数据。我想知道如何将这些 类 分组并相应地显示。

例如,我在页面上有注册表单,其中包含日期和 类。 所以我想先按日期对它们进行分组,而不是 classes.My registration 模型具有如下字段。

Registration
id
event_id
dates
class_id
user_id
title
description

现在在这个视图文件上,我正在传递 $modelRegistration 的数组,所以我实际上没有简单的多维数组,所以我不能使用该逻辑对其进行分组,display.Here 是我要显示的视图代码forms

<table class="table table-bordered table-striped">
<thead>
<tr>
    <th>Date</th>
    <th>Class</th>
    <th>User Name</th>
    <th>Title</th>
    <th>Description</th>
</tr>
</thead>
<tbody class="container-items">


<?php foreach ($modelsRegistration as $indexRegistration => $modelRegistration): ?>
    <tr value="<?= $modelRegistration->date ?>" class="house-item">
        <td class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><?= $modelRegistration->class->name ?></td>
        <td class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
            <label><?= $modelRegistration->user->name ?></label>
        <td class="col-lg-2 col-md-2 col-sm-2 col-xs-2"><?= $modelRegistration->title ?> </td>
        <td class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><?= $modelClass->description ?> </td>
    </tr>
<?php endforeach; ?>
</tbody>

我想得到如下数组

Registration
(
[0] => 10/12/2015
(
    [0] => class-1(
              [0] => array(
                      [title] => "Title 1"
                      [id] => 5
                      [username] => John 
                      [Description] => Team Leader
                     )
              [1] => array(
                      [title] => "Title 2"
                      [id] => 6
                      [username] => John 1 
                      [Description] => Team 2
                     )
     )
    [1] => class-2(
              [0] => array(
                      [title] => "Title 4"
                      [id] => 7
                      [username] => John 4 
                      [Description] => Team 4
                     )
              [1] => array(
                      [title] => "Title 5"
                      [id] => 9
                      [username] => John 5 
                      [Description] => Team 6
                     )
     )
    [2] => class-3(
              [0] => array(
                      [title] => "Title 1"
                      [id] => 5
                      [username] => John 
                      [Description] => Team Leader
                     )
              [1] => array(
                      [title] => "Title 2"
                      [id] => 6
                      [username] => John 1 
                      [Description] => Team 2
                     )
     )
)
[0] => 12/12/2015
(
     [0] => class-1(
              [0] => array(
                      [title] => "Title 8"
                      [id] => 12
                      [username] => John 10
                      [Description] => Team 77
                     )
              [1] => array(
                      [title] => "Title 27"
                      [id] => 67
                      [username] => John 17 
                      [Description] => Team 27
                     )
     )
    [1] => class-3(
               [0] => array(
                      [title] => "Title 41"
                      [id] => 71
                      [username] => John 41 
                      [Description] => Team 41
                     )
              [1] => array(
                      [title] => "Title 51"
                      [id] => 59
                      [username] => John 54 
                      [Description] => Team 64
                     )
     )
)

如果我可以先按日期分组,然后 类 比我只想显示一次日期,而不是在该日期下所有分组 类 和 类 我可以显示其他information.Thank你

假设您的模型名称是Registration,视图名称是vrego。

  1. 在您的控制器中

    // select 所有并按日期排序和 class_name

    $registrations= Registration::find()
                    ->orderBy('date, class_name')
                    ->all();
    
    return $this->render('vrego', [
                    'registrations' => $registrations,
                ]);
    
  2. 然后在您的视图中循环遍历 $registrations 并显示每条记录。

使用 date 组映射您的模型数据 Array helper Map Group in yii2

$model = Registration::findAll();
$registrationModels = ArrayHelper::map($model , 'event_id' ,'class_id' ,'dates');

我得到了我自己尝试的结果: 您需要创建一个 class 注册,我认为您创建了那个然后使用下面的查询您将获得该信息。

 $events  = Registration::find()->all();
        foreach($events as $event){
            $new_array[$event->dates_date][][$event->class_id][]=$event;
        }
<pre>
after print_r($new_array);
<pre>