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’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 yiidbActiveQuery
*/
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 yiiwebView $this */
use commonwidgetsAlert;
use frontendassetsAppAsset;
use yiibootstrap5Breadcrumbs;
use yiihelpersHtml;
use yiiwidgetsListView;
$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.