2

I am trying to generate CRUD in yii2 using Gii. I have created a model class using Gii and it is created successfully. But When I try to create CRUD, it gives the following error:

Class 'app\models\UserRoles' does not exist or has syntax error.

Here is the screen shot of what I am filling the form with and error: enter image description here The class UserRoles exists and there is certainly no syntax error. I have tried this and it doesn't work either. Help would be highly appreciated.

Community
  • 1
  • 1
Saani
  • 791
  • 7
  • 28
  • 68

5 Answers5

2

This because the class i gii can't revolve properly the app\models\YourClassName (i have experienced because create most of models in common\models for share the models between frontend and backend)

be sure where you have save the models (could be in backend\models)

then set the proper value in

Model Class eg:

 backend\models\YourClassName  

or simply

 YourClassName 

And do the same for Search Model Class too

In viewpath you should use a proper alias so if you are in frontent

  @frontend\views\your-model-name

or (in some case) you can set the abolute path to the directory you need

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • thanks for the answer, but the model is created in `frontend` and I am trying to create the `CRUD` in `frontend` as well. Still gives the error. – Saani Nov 07 '16 at 07:25
  • Have you tried to set frontend\models\YourClassName in the Model class field ? – ScaisEdge Nov 07 '16 at 07:26
  • Yes I did and it gave the exception first in the view path and then I tried it without `@` and it created but in the `web\app\` directory. – Saani Nov 07 '16 at 07:31
  • If i understand right the message Class 'app\models\UserRoles' does not exist or has syntax error. don't appear but you have a new error on the view path ? please confirm – ScaisEdge Nov 07 '16 at 07:35
  • When I tried `frontend\models\UserRoles`, it did not give any error like `Class 'app\models\UserRoles' does not exist or has syntax error.` but gave error in view path. and then I removed the '@' from view path and it created the CRUD. But it created the views in `web\app\userroles` directory. I copied the folder and moved it to the desired location and it is working now. – Saani Nov 07 '16 at 07:38
  • I have update the answer with a suggestion for viewpath too – ScaisEdge Nov 07 '16 at 07:42
  • Thanks for your help! Got it now! – Saani Nov 07 '16 at 07:44
  • @Saani well if my answer is right please mark it as accepted .. otherwise .. let me know if there are problems – ScaisEdge Nov 07 '16 at 08:35
1

On the Model geneneration page change the namespace to the appropriate application. ie, change from app\models to either backend\models or frontend\models.

change the namespace here

desertnaut
  • 57,590
  • 26
  • 140
  • 166
0

Check namaspace of your model class. add Model class

app\models\YourClassName  

add controller class

backend\controllers\Controller

view path

@backend/views/foldername
Nilesh Patil
  • 138
  • 1
  • 1
  • 10
0

I try to create crud in console with the following command:

php yii gii/crud --controllerClass=app\controllers\PostController --modelClass=app\models\Post

and get the same error. Finally, I run it successfully in this way:

php yii gii/crud --controllerClass=app\\controllers\\PostController --modelClass=app\\models\\Post
0

It's Showing that your Model class does not exist in the Model folder so First Create Model By gii -> Model Generator

  1. Select Table Name (drop-down list will generate Automatically).
  2. Enter Model Class Name.
  3. Click on Preview -> generate