I know this might seem trivial but I really can't figure it out. I'm starting to develop a database application using Yii Framework and MySQL. I tried following the simple basic tutorial :
http://www.yiiframework.com/doc-2.0/guide-start-databases.html
, but I used my own table "Supermarkets".
I'm getting this error:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: app\models\Supermarkets::name
It's obvious that the method get ('name') is causing this error, but I don't know how to fix this.
This is my code:
…models/supermarkets.php:
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Supermarkets extends ActiveRecord
{
}
…controllers/SupermarketsController.php:
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Supermarkets;
class SupermarketsController extends Controller
{
public function actionIndex()
{
$query = Supermarkets::find();
$pagination = new Pagination([
'defaultPageSize' => 5,
'totalCount' => $query->count(),
]);
$supermarkets = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index', [
'supermarkets' => $supermarkets,
'pagination' => $pagination,
]);
}
}
…views/Supermarkets/index.php:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Supermarkets</h1>
<ul>
<?php foreach ($supermarkets as $supermarket): ?>
<li>
<?= $supermarket->name?>
<?= $supermarket->location ?>
<?= $supermarket->telephone ?>
<?= $supermarket->fax ?>
<?= $supermarket->website ?>
</li>
<?php endforeach; ?>
</ul>
<?= LinkPager::widget(['pagination' => $pagination]) ?>
Supermarkets.db:
CREATE TABLE IF NOT EXISTS `supermarkets` (
`Name` varchar(71) NOT NULL,
`Location` varchar(191) DEFAULT NULL,
`Telephone` varchar(68) DEFAULT NULL,
`Fax` varchar(29) DEFAULT NULL,
`Website` varchar(24) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Any suggestions please?
Best Answer
May be you lose in model
Supermarkets
:If you not set method, default table-name will be
supermarkets
. Because inyii\db\ActiveRecord
set:EDIT
Use delete this from your model
And use
Or better way. Use your first code. And change columns -> set small first letter. Like that