Yii2 在手风琴小部件中显示动态 GridView

Yii2 Display Dynamic GridView inside Accordion Widget

我是 Yii2 的新手,我被困在一个相当琐碎的任务上。

我有两个 tables/models(实体和 属性)。一个实体可以有多个属性,但一个 属性 只能有一个实体。这些关系已在各自的模型中定义

我正在尝试为已登录的用户创建一个仪表板视图,以显示与该用户关联的实体(通过联结 table Entityuser)。在视图中,我希望为每个实体显示一个手风琴,展开时将显示属于该实体的属性的 GridView。我遇到的问题是 GridView 只显示 ArrayDataProvider 返回的数组中的最后一个 属性 模型,而不管实体关系如何(即它还在所有实体的手风琴中显示相同的 属性 模型).

我已经在下面复制了我的代码。任何帮助将不胜感激。我在这上面花了太多时间

型号:

实体

public function getProperty()
{
    return $this->hasMany(Property::className(), ['entity_id' => 'entity_id']);
}

属性

public function getEntity()
{
    return $this->hasOne(Entity::className(), ['entity_id' => 'entity_id']);
}

控制器:

public function actionDashboard($id)
{
    $model = User::findOne($id);

    $entities = new ActiveDataProvider([
            'query' => Entity::find()
                        ->joinWith('entityUsers')
                        ->where(['entity_user.user_id' => $id]),
            'pagination' => [
            'pageSize' => 10,
        ],
    ]);


  foreach($entities->models as $ent){

  $en_id = $ent->entity_id;

  $dataProvider = new ArrayDataProvider([
       'models' => Property::find()->where(['entity_id' => $en_id])->all(),
       // 'models' => $searchModel->entity_id,
        'pagination' => [
            'pageSize' => 10,
        ],
    ]);
   }        

    return $this->render('userProfile', [
    
    'model' => $model,
    'entities' => $entities,
    'dataProvider' => $dataProvider,

查看:

foreach($entities->models as $model){

$entity_id = $model->entity_id;
   
echo Accordion::widget([
    'items' => [
    ['header' => $model->entity,
    'content' => 
                 
   GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    'name',
    'address',
    'city',
    'entity_id',
    'land',
     ['class' => 'yii\grid\ActionColumn',
     'header' => '',
     'template' => '{view}',
     'controller' => 'property',
     'contentOptions' => ['style' => 'width:50px; white-space: normal;'],
    ]]
    ])
    ]
    ],
    'clientOptions' => ['collapsible' => true, 'active' => false]
    
    ]); 
     
    }
   }

您只为最后一条记录制作了 $dataProvder

控制器

foreach ($entities->models as $ent) {
    $en_id = $ent->entity_id;
    $dataProviders[$en_id] = new ArrayDataProvider([
       'models' => Property::find()->where(['entity_id' => $en_id])->all(),
       'pagination' => [
           'pageSize' => 10,
        ],
    ]);
}
return $this->render('userProfile', [
    'model' => $model,
    'entities' => $entities,
    'dataProviders' => $dataProviders,
]);

查看

foreach($entities->models as $model){
   $entity_id = $model->entity_id;
   $dataProvider = $dataProviders[$entity_id];

   echo Accordion::widget([
    .
    .
    .
    .
    .