Yii2 gii crud error in index.php

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

I encountered the same issue on a recent gii code generation. The latest release of Yii has a bug in the code generation - I think. The yii\grid\ActionColumn column used to be generated as simply ['class' => 'yii\grid\ActionColumn'], and everything worked fine - and still does if you want to use that instead. In fact, I replaced the generated code with the legacy code generated to keep it consistent for my project. And you are correct about simply removing the class name to make this work. I didn’t dig into this too much as I simply went back to the old way.

And Welcome to Yii! There is a lot of hype out there about other frameworks and the truly smart ones discover that Yii is the Framework where all the magic happens. :grinning:

Looks like a name space problem. The CRUD generator seems to fail adding necessary use statements in the output files.

use app\models\Country;
use yii\grid\ActionColumn;
1 Like

Thanks for your answers!
It’s nice to know that you can count on someone’s help when you are starting to face something completely new and unknown