Goodmorning everyone! I am new to both the forum and using yii.
I’m working my way through “The Definitive Guide to Yii 2.0”. I followed the tutorial, except in the use of the rdbms (I use postgres instead of mysql) without problems.
When I use gii everything is ok: it creates for me models controller and views, but when I try to access index.php with this URL:http://basicyii/index.php?r=country/index I have this error:
TypeError: Argument 2 passed to yii\base\View::{closure}() must be an instance of Country, instance of app\models\Country given in C:\Apache24\htdocs\basic\views\country\index.php:36
With his stack trace
This is line 36 that generates the error:
'urlCreator' => function ($action, Country $model, $key, $index, $column) {
And thi in the GridWiev widget that contains this line and is generated by gii:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
'population',
[
'class' => ActionColumn::className(),
'urlCreator' => function ($action, Country $model, $key, $index, $column) {
return Url::toRoute([$action, 'code' => $model->code]);
}
],
],
]); ?>
If I remove the class name Country before $model everything works
Now my Country model is in C:\Apache24\htdocs\basic\models but it seems like the $model passed to ‘urlCreator’ => function isn’t a Country
This is my environment:
Windows 10
Apache 2.4
PHP 7.4.27
YII 2.0.44 installed via composer
Can you help me? I’d like to understand what happens
thank you
UPDATE
doing some tests I saw that:
using app\models\Country instead of Country in ‘urlCreator’ => function ($action, Country $model, $key, $index, $column) {
OR
adding use app\models\Country;
Everything works… but I’d like to understand why… I would be grateful if you could help me I’m not so skilled also with PHP 7