Yii模块内生成CRUD

刚接触Yii框架不太了解其中内容,做一个简单的讲解,以便以后使用。

操作

  1. module 命令生成 module

  2. config中设置读取module

  3. model 命令生成 model

  4. crud 命令生成 CRUD

具体如下

先在数据库中做一个表

create table customers( 
  id int primary key auto_increment, 
  name varchar(255), 
  zip varchar(7), 
  tel varchar(30), 
  address varchar(255), 
  email varchar(255), 
  birthday datetime 
) Engine=InnoDB;


生成module

$ ./protected/yiic shell
Yii Interactive Tool v1.1 (based on Yii v1.1.0)
Please type 'help' for help. Type 'exit' to quit.
>> module kokyaku
      mkdir /home/nakano/yii/testdrive/protected/modules
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku/views
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku/views/default
   generate views/default/index.php
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku/views/layouts
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku/controllers
   generate controllers/DefaultController.php
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku/messages
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku/components
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku/models
   generate KokyakuModule.php

Module 'kokyaku' has been created under the following folder:
    /home/nakano/yii/testdrive/protected/modules/kokyaku

You may access it in the browser using the following URL:
    http://hostname/path/to/index.php?r=kokyaku

Note, the module needs to be installed first by adding 'kokyaku'
to the 'modules' property in the application configuration.

>> exit


设置config

'name'=>'test app',
'modules'=> array( 'kokyaku' ), // この行を追加する。複数モジュールを使う場合には array()の中に列挙


生成model和crud

$ ./protected/yiic shell
Yii Interactive Tool v1.1 (based on Yii v1.1.0)
Please type 'help' for help. Type 'exit' to quit.
>> model kokyaku.models.Customer customers
   generate models/Customer.php
   generate fixtures/customers.php
   generate unit/CustomerTest.php

The following model classes are successfully generated:
    Customer

If you have a 'db' database connection, you can test these models now with:
    $model=Customer::model()->find();
    print_r($model);

>> crud kokyaku.models.Customer 
   generate CustomerController.php
   generate CustomerTest.php
      mkdir /home/nakano/yii/testdrive/protected/modules/kokyaku/views/customer
   generate create.php
   generate update.php
   generate index.php
   generate view.php
   generate admin.php
   generate _form.php
   generate _view.php

Crud 'customer' has been successfully created. You may access it via:
http://hostname/path/to/index.php?r=kokyaku/customer


完成了。


你可能感兴趣的:(PHP,yii,crud,yiic)