Code Generation using Command Line Tools (deprecated)

Note: The code generators in yiic shell have been deprecated since version 1.1.2. Please use the more powerful and extensible Web-based code generators available in Gii, instead.

Open a command line window, and execute the commands listed as follows,

  1. % cd WebRoot/testdrive
  2. % protected/yiic shell
  3. Yii Interactive Tool v1.1
  4. Please type 'help' for help. Type 'exit' to quit.
  5. >> model User tbl_user
  6. generate models/User.php
  7. generate fixtures/tbl_user.php
  8. generate unit/UserTest.php
  9.  
  10. The following model classes are successfully generated:
  11. User
  12.  
  13. If you have a 'db' database connection, you can test these models now with:
  14. $model=User::model()->find();
  15. print_r($model);
  16.  
  17. >> crud User
  18. generate UserController.php
  19. generate UserTest.php
  20. mkdir D:/testdrive/protected/views/user
  21. generate create.php
  22. generate update.php
  23. generate index.php
  24. generate view.php
  25. generate admin.php
  26. generate _form.php
  27. generate _view.php
  28.  
  29. Crud 'user' has been successfully created. You may access it via:
  30. http://hostname/path/to/index.php?r=user

In the above, we use the yiic shell command to interact with ourskeleton application. At the prompt, we execute two sub-commands: model User tbl_userand crud User. The former generates a model class named User for the tbl_user table,while the latter analyzes the User model and generates the code implementingthe corresponding CRUD operations.

Note: You may encounter errors like "…could not find driver", even though the requirement checker shows you have already enabled PDO and the corresponding PDO driver. If this happens, you may try to run the yiic tool as follows,
  1. % php -c path/to/php.ini protected/yiic.php shell
where path/to/php.ini represents the correct PHP ini file.

Let's enjoy our work by browsing the following URL:

  1. http://hostname/testdrive/index.php?r=user

This will display a list of user entries in the tbl_user table.

Click the Create User button on the page. We will be brought to the loginpage if we have not logged in before. After logged in, we seean input form that allows us to add a new user entry. Complete the form andclick on the Create button. If there is any input error, a nice errorprompt will show up which prevents us from saving the input. Back to theuser list page, we should see the newly added user appearing in the list.

Repeat the above steps to add more users. Notice that user list pagewill automatically paginate the user entries if there are too many to bedisplayed in one page.

If we login as an administrator using admin/admin, we can view the useradmin page with the following URL:

  1. http://hostname/testdrive/index.php?r=user/admin

This will show us the user entries in a nice tabular format. We can click on the tableheader cells to sort the corresponding columns. We can click on the buttonson each row of data to view, update or delete the corresponding row of data.We can browse different pages. We can also filter and search to look forthe data we are interested in.

All these nice features come without requiring us to write a single lineof code!

User admin page
User admin page

Create new user page
Create new user page

原文: https://www.yiiframework.com/doc/guide/1.1/zh-cn/quickstart.first-app-yiic