以 yii2-dynamic 形式创建 actionView

Creating actionView in yii2-dynamic form

我正在研究 yii2。我使用 wbraganca 创建了一个动态表单 / yii2-动态形式。现在我想实现视图。在给定的扩展中,没有实现视图的过程。它只显示 action method of view。我已经尝试实现,但无法完全实现。

控制器代码

 /**
 * Finds the MdcTariffSlabs model based on its primary key value.
 * If the model is not found, a 404 HTTP exception will be thrown.
 * @param integer $id
 * @return MdcTariffSlabs|\yii\db\ActiveQuery
 * @throws NotFoundHttpException if the model cannot be found
 */
protected function findModelSlabs($id)
{
    if (($model = MdcTariffSlabs::find()->where(['t_id'=>$id])) !== null) {

        return $model;
    }

    throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}

 public function actionView($id)
{
    $model = $this->findModel($id);
    $modelTarrifSlabs = $this->findModelSlabs($model->id);


    return $this->render('view', [
        'model' => $model,
        'modelTarrifSlabs' => $modelTarrifSlabs,
    ]);
}

查看

/* @var $this yii\web\View */
/* @var $model common\models\MdcTariff */
/* @var $modelTarrifSlabs \common\models\MdcTariffSlabs */
.
.
.
.
.
.
.
<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
        'id',
        't_name',
        'created_by',
        'created_at',
    ],
]) ?>

<?= DetailView::widget([
    'model' => $modelTarrifSlabs,
    'attributes' => [
        'id',
        't_name',
        'created_by',
        'created_at',
    ],
]) ?>

创建后我正在渲染我的视图并收到错误

Getting unknown property: yii\db\ActiveQuery::id

 var_dump($modelTarrifSlabs);
    exit();

上面的代码给了我

object(yii\db\ActiveQuery)#131 (33) { ["sql"]=> NULL ["on"]=> NULL ["joinWith"]=> NULL ["select"]=> NULL ["selectOption"]=> NULL ["distinct"]=> NULL ["from"]=> NULL ["groupBy"]=> NULL ["join"]=> NULL ["having"]=> NULL ["union"]=> NULL ["withQueries"]=> NULL ["params"]=> array(0) { } ["queryCacheDuration"]=> NULL ["queryCacheDependency"]=> NULL ["_events":"yii\base\Component":private]=> array(0) { } ["_eventWildcards":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } ["where"]=> array(1) { ["t_id"]=> int(1) } ["limit"]=> NULL ["offset"]=> NULL ["orderBy"]=> NULL ["indexBy"]=> NULL ["emulateExecution"]=> bool(false) ["modelClass"]=> string(28) "common\models\MdcTariffSlabs" ["with"]=> NULL ["asArray"]=> NULL ["multiple"]=> NULL ["primaryModel"]=> NULL ["link"]=> NULL ["via"]=> NULL ["inverseOf"]=> NULL ["viaMap":"yii\db\ActiveQuery":private]=> NULL }

我认为你的错误是因为你得到了一个活动查询而不是模型。这就是错误所说的:

Getting unknown property: yii\db\ActiveQuery::id

试试这个:

改变这个:

protected function findModelSlabs($id)
    {
        if (($model = MdcTariffSlabs::find()->where(['t_id'=>$id])) !== null) { //<---- here i guess you have error
    
            return $model;
        }
    
        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }

为此:

protected function findModelSlabs($id)
{
    if (($model = MdcTariffSlabs::find()->where(['t_id'=>$id])->one()) !== null) { 
        return $model;
    }

    throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}