Yii 2 Gii Problem

Hi All,

I have installed Yii2 in a folder called yii2.

I have created a table called "posts"

I have created a model called "Posts".

Now I am in GII and entered this:

Model Class

yii2\models\Posts

Search Model Class

yii2\models\search\PostsSearch

Controller Class

yii2\Controllers\PostsController

But I am getting an error for each field:

-Class ‘yii2\models\Posts’ does not exist or has syntax error.

-The class namespace is invalid: yii2\models\search

-The class namespace is invalid: yii2\Controllers

Any idea why please?

Also why has Yii2 the full paths for all the classes? Strange and gives more work no?

Thank you,

Ben

You did not mention whether you are using a basic app or an advanced app. Your namespaces seem to point to invalid path aliases.

For an advanced app you can use something like this (if you want classes in the frontend):


frontend\models\Posts

or


frontend\controllers

or for basic use something like this:


app\models\Posts

or


app\controllers

Thank you for the reply, I am using the basic package, whatis the difference between both please as they donot explain it on the download page.

Following your comment, it should have worked then.

Just tomake sure, by "app" you mean the folder in which I have dropped all my Yii files, am I correct?

Or do I still need to add "app" anyway?

Thank you,

Ben

Read HERE about yii 2 basic app and advanced app.

These are predefined aliases for your app paths - which come predefined with your basic or advanced app. For example @app, @frontend, @common, @backend. You may want to read about these and use them. You need to use them in your namespaces. Else you may create your own path alias using Yii::setAlias.

You may want to read this FIRST if you are coming over from Yii 1.x to Yii 2.

Just added a wiki listing these aliases with the yii2 basic and advanced app.

It’s in the guide btw.

My bad (must have missed it)… you folks are too good.

Guide is changed too fast nowadays :)

Thanks much for this. Gives me a headstart for my app development.

Thanks you were right, I added app\ and it worked.

But there is something I really find strange in Yii 2, it takes a longtime to type these urls, why there is not an automatic system that calculate them in ajax for us or something similar?

I feel like Gii regressed from Yii 1, no? It was much easier to create a CRUD before.

Ben

I have a similar problem as well. (Qualifier - I am new to using Yii and Gii)

I have yii2 advanced installed. I can create a model just fine using Gii, but when I run the CRUD Generator I consistently get this error:

Class ‘backend\models\Dealers’ does not exist or has syntax error.

Here is what I have in the Model Generator:

Table Name

dealers

[b]

Model Class[/b]

Dealers

[b]

Namespace[/b]

backend\models\Dealers

The model generates successfully and pulls the database table fields into the code as expected:

namespace backend\models\Dealers;

use Yii;

/**

  • This is the model class for table "dealers".

  • @property integer $d_id

  • @property string $d_name

  • @property string $d_type

  • @property string $d_url

  • @property string $d_address

  • @property string $d_address2

  • @property string $d_city

  • @property string $d_state

  • @property integer $d_zip

  • @property string $d_country

  • @property string $d_phone

  • @property string $d_contact

Then I move on to the CRUD Generator with the following:

[b]

Model Class[/b]

backend\models\Dealers

Search Model Class

backend\models\DealersSearch

Controller Class

backend\controllers\DealersController

[b]

View Path[/b]

@backend/views/dealers

When I click preview, I get the error no matter how I change the names and paths (Class ‘backend\models\Dealers’ does not exist or has syntax error.)

I checked the file structure and there is indeed a file located at backend/models/Dealers/Dealers.php. I’m not sure where to go from here, It seems like I have done everything correctly. Hoping I can get past this error and get to know Yii better as I would like to build an upcoming project using it.

Thanks in advance for your assistance!

jm

Just found my problem:

I had backend\models\Dealers in the namespace…

should have been backend\models

jm

Hi all,

I had same problem in solved by these steps:

  1. check your model namespace

  2. change (app or yii2) to (fronted or backend)

namespace app\models; ==> namespace backend\models;

Hope Regard …