Search code examples
yii2yii2-advanced-appyii2-modeldynamic-forms

Creating actionView in yii2-dynamic form


I am working on yii2. I have created a dynamic form using wbraganca / yii2-dynamicform. Now I want to implement the view. In the given extension there is no procedure that has implemented a view. It just shows the action method of view. I have tried to implement but couldn't able to do it completely.

Controller Code

 /**
 * 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,
    ]);
}

View

/* @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',
    ],
]) ?>

After creating I am rendering my view and getting an error

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

enter image description here

 var_dump($modelTarrifSlabs);
    exit();

The above code gives me

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 }

Solution

  • I think your error is because you get an Active Query instead of Model. And that what the error says:

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

    Try this:

    Change this:

    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.'));
        }
    

    To this:

    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.'));
    }