2015-12-10 121 views
0

嗨,大家好,我有一個頁面,我使用foreachmodels開始循環顯示數據。我想知道如何分組這些類並相應地顯示。如何在yii2中對模型數組進行分組

例如我有登記表格在頁面上有日期和類。 所以我想按日期先將它們分組,然後再按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 
        ) 
    ) 
) 

如果我可以按照日期先分組,然後再按班分類,比我只想顯示日期的一次,並且在該日期之下,所有分組班級和班級下我都可以顯示其他信息。謝謝

+0

你可以發表你想要的數組的例子嗎? – arogachev

+0

@arogachev我已更新問題 –

+0

如何確定日期和班級? – arogachev

回答

2

地圖使用dateArray helper Map Group in yii2

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

這是可以幫助我的東西。但爲此,我必須將我的'$ modelsRegistrations'轉換爲數組? –

+0

然後使用'$ model = Registration :: find() - > asArray() - > all();' –

+0

那就好了。我已經做到了,但現在我應該如何處理相關數據?例如,當我使用'$ model'時,我可以執行'$ model-> user-> name'來獲取用戶名,但是當我將它轉換爲數組時,我只能獲取id。那麼我應該在這裏修改我的查詢還是有其他方法? –

0

假設您的模型名稱是註冊並且視圖名稱是vrego。

  1. 在你的控制器

    //選取所有和按日期和CLASS_NAME

    $registrations= Registration::find() 
           ->orderBy('date, class_name') 
           ->all(); 
    
    return $this->render('vrego', [ 
           'registrations' => $registrations, 
          ]); 
    
  2. 然後在您的視圖遍歷$註冊並顯示每個記錄。

0

你的模型我正在歌廳的結果我想我自己: 你需要創建一個類註冊的,我想你創建了一個,然後使用這下面的查詢,你會得到這個信息。

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

謝謝。但在此之後,我想通過'class_id'接下來進行分組。那麼我應該在那裏創建另一個循環? 'class_id'? –

+0

嗨我編輯的代碼,請現在檢查 –

+0

好吧,讓我試試這個以及 –

相關問題