yii2yii2-model

Yii2 passing of Data from model to view throws undefined array key exception even after render


In Yii Frontend, I have learned that to pass the data from Model to view, you have to use the siteController or a dedicated Controller then make sure they are included in the data array passed to the render method.

However the dashboard.php (view) throws error Undefined array key "dataProvider" the vardump shows there is data on my actionDashboard but does not show this data in dashboard view when I use vardump.

here is the structure of my folders

project folder

-common\models\ReadingData.php

-frontend\controllers\siteController.php

-frontend\views\site\dashboard.php

Here's the summary of what I have been doing: I am trying to add data to my view fetched from the models.

I focused on SiteController and dashboard.php(view): Verified passing data ($dataProvider and $dataCount) from my SiteController's actionDashboard method to my dashboard.php view and make sure they are included in the data array passed to the render method.

For Now I focused on using SiteController to make things easier for me to understand and to make it work. before I create a dedicated dashboardController.

I have a ReadingData Model with just 2 functions:

   public function getAccount()
{
    return $this->hasOne(Account::class, ['id' => 'account_id']);
}

/**
 * Gets query for [[ReadingSchedule]].
 *
 * @return \yii\db\ActiveQuery
 */
public function getReadingSchedule()
{
    return $this->hasOne(ReadingSchedule::class, ['id' => 'reading_schedule_id']);
}

a siteController

   /**
 * Displays dashboard page.
 *
 * @return mixed
 */
public function actionDashboard()
  {
      $dataProv = new ActiveDataProvider([
          'query' => ReadingData::find(),
      ]);

       $dataCnt = $dataProv->getTotalCount();

      
          var_dump($dataCnt);
    
      
          return $this->render('dashboard', [
              'dataProvider' => $dataProv, // For ListView or other data widgets
              'dataCount' => $dataCnt,        // Custom data
              
          ]);
  }

and a dashboard.php view

 <?php

/** @var yii\web\View $this */
use common\widgets\Alert;
use frontend\assets\AppAsset;
use yii\bootstrap5\Breadcrumbs;



use yii\helpers\Html;
use yii\widgets\ListView;

$this->title = 'Dashboard';
$this->params['breadcrumbs'][] =  $this->title;

<div class="col-md-2 d-flex flex-column flex-shrink-0 p-3 bg-light" style="width: 950px;">
        <main role="main" class="flex-shrink-0 ms-auto">
        
            
        <div class="container">
            
            <?= Alert::widget() ?>
            <h1><?= Html::encode($this->title) ?></h1>

            <?php
                var_dump($this->params);
echo "Total Reading Data: " . $this->params['dataCnt'];

In siteController the var_dump($dataCnt); shows count data int(2) which is because I have 2 rows of data from my table.

however on dashboard.php, var_dump($this->params);only return values that I passed here

$this->params['breadcrumbs'][] =  $this->title;

which I passed at the beginning of the dashboard.php file and throws undefined here

 echo "Total Reading Data: " . $this->params['dataCnt'];

I tried renaming them because I thought i might be using reserved keywords but it still throws the same error message.


Solution

  • When passing parameters to view like this

    return $this->render('dashboard', [
        'dataProvider' => $dataProv,
        'dataCount' => $dataCnt,
    ]);
    

    The passed parameters will become available as variables $dataProvider and $dataCount. They won't be set in yii\base\View::$params. So you should try this:

    echo "Total Reading Data: " . $dataCount;
    

    Note that the variable name matches the key in array provided when passing parameters to render() method.

    The yii\base\View::$params is for sharing params between different view templates. For example you are setting $this->params['breadcrumbs'][] = $this->title; in specific view template depending on current action. Then it is used in layout template to generate breadcrumbs on each page.