Trying to get propertyof non-object

I am starting to learn Yii framework so I am a beginner. I am struggling. I want to fetch the data from database using yii2 framework. This is my controller

 public  function actionView()
{


    $this->view->title = 'List Hotels';
    $items = ArrayHelper::map(Hotel::find()->all(), 'id', 'name');

        return $this->render('index', [
            'items' => $items,


        ]);

}

In my view file, I used the fetched data as below;

   <?php

/* @var $this yii\web\View */

use yii\helpers\Html;

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

<?php foreach ($items as $item): ?>

<p> <?= $item-> name ?></p>
<p> <?= $item->address ?></p>
<p> <?= $item->description ?></p>


<?php endforeach; ?>

When I wrote var_dumps($items) under $items I can see the datas. However in the view It says Trying to get property ‘name’ of non-object. What did I wrong here please guide me. THanks for your time.

Remove ArrayHelper::map( … )

1 Like

Thanks it works. Why I shoul not use Array helper here?

I don’t understand why you added it.
My guess is that you should next read this section of the Guide
https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#querying-data

Unfortunately there are no demo projects using db coming with Yii 2.0 (as with 1.1 IIRC).
Books available: https://www.yiiframework.com/books
Also check other resources: https://www.yiiframework.com/resources

1 Like

ArrayHelper::map returns an array of arrays, but what you wanted is an array of objects.

1 Like