cakephp bake

cake php官网:http://cakephp.org/

使用bake生成代码.

1:首先你要把数据库建好,例如我们使用PowerDesigner把数据库设计好,导出sql语句,在mysql中创建数据库tfss,创建两个简单的表(特别需要注意表名以及外键的命名,请参考cakephp的命名规范):
   drop table if exists admingroups;
   /*==============================================================*/
   /* Table: admingroups                                           */
   /*==============================================================*/
   create table admingroups
   (
       id                   int not null auto_increment comment '管理组ID',
       ac_user              int not null default 1 comment '允许创建用户(0: false ; 1: trure)',
       ac_class             int not null default 1 comment '允许创建班级组别(0: false ; 1: trure)',
       primary key (id)
   );
   alter table admingroups comment '管理组表';

   drop table if exists usergroups;
   /*==============================================================*/
   /* Table: usergroups                                            */
   /*==============================================================*/
   create table usergroups
   (
       id                   int not null auto_increment comment '用户组ID',
       admingroup_id        int not null default 0 comment '用户组类型1:学生;2:校长;3:老师)',
       group_name           varchar(50) not null default '' comment '用户组名称(管理组为学生的:中1~中6总共6个级别)',
       user_count           int not null default 0 comment '用户数量',
       primary key (id)
   );
   alter table usergroups comment '用户组表';

2: 首先要将php的php.exe加入环境变量中,例如我的php安装在E:\work\Server\PHPCore下,将该路径加入到path中

3: 在命令行里面执行如下命令:php cakephp的bake文件路径 bake
   例如你的cakephp解压文件全部放在
   “E:/work/PHPPro/CakePHPTest/”目录下
   那么则在命令行执行: php E:/work/PHPPro/CakePHPTest/lib/Cake/Console/cake.php bake

   1)  首先要知道相关命令:
    [D]atabase Configuration
    [M]odel
    [V]iew
    [C]ontroller
    [P]roject
    [F]ixture
    [T]est case
    [Q]uit


   2):执行了以上命令,开始配置数据库
   Welcome to CakePHP v2.1.0 Console
   ---------------------------------------------------------------
   App : app
   Path: E:\work\PHPPro\CakePHPTest\app\
   ---------------------------------------------------------------
   Your database configuration was not found. Take a moment to create one.
   ---------------------------------------------------------------

   ---------------------------------------------------------------
   Database Configuration:
   ---------------------------------------------------------------
   Name:
   [default] > default
   Datasource:(Mysql/Postgres/Sqlite/Sqlserver)
   [Mysql] > mysql
   Persistent Connection? (y/n)
   [n] > n
   Database Host:
   [localhost] > localhost
   Port?
   [n] > 3306
   User:
   [root] > root
   Password:
   > 000000
   Database Name:
   [cake] > tfss
   Table Prefix?
   [n] > n
   Table encoding?
   [n] > n

    ---------------------------------------------------------------
    The following database configuration will be created:
    ---------------------------------------------------------------
    Name:         default
    Driver:       mysql
    Persistent:   false
    Host:         localhost
    Port:         3306
    User:         root
    Pass:         ****
    Database:     tfss
    ---------------------------------------------------------------
    Look okay? (y/n)
    [y] > y
    Do you wish to add another database configuration?
    [n] > n

    Creating file E:\work\PHPPro\CakePHPTest\app\Config\database.php
    Wrote 'E:\work\PHPPro\CakePHPTest\app\Config\database.php'

    3)使用bake all生成m,v,c
    我们在命令行敲入: php E:/work/PHPPro/CakePHPTest/lib/Cake/Console/cake.php bake all
    执行如下:

    Welcome to CakePHP v2.1.0 Console
    ---------------------------------------------------------------
    App : app
    Path: E:\work\PHPPro\CakePHPTest\app\
    ---------------------------------------------------------------
    Bake All
    ---------------------------------------------------------------
    Possible Models based on your current database:    
    1. Admingroup
    2. Usergroup
    Enter a number from the list above,
    type in the name of another model, or 'q' to exit
    [q] > 1

    Baking model class for Admingroup...

    Creating file E:\work\PHPPro\CakePHPTest\app\Model\Admingroup.php
    Wrote `E:\work\PHPPro\CakePHPTest\app\Model\Admingroup.php`
    PHPUnit is not installed. Do you want to bake unit test files anyway? (y/n)
    [y] > n

    Baking controller class for Admingroups...

    Creating file E:\work\PHPPro\CakePHPTest\app\Controller\AdmingroupsController.php
    Wrote `E:\work\PHPPro\CakePHPTest\app\Controller\AdmingroupsController.php`
    PHPUnit is not installed. Do you want to bake unit test files anyway? (y/n)
    [y] > n

    Baking `index` view file...

    Creating file E:\work\PHPPro\CakePHPTest\app\View\Admingroups\index.ctp
    Wrote `E:\work\PHPPro\CakePHPTest\app\View\Admingroups\index.ctp`

    Baking `view` view file...

    Creating file E:\work\PHPPro\CakePHPTest\app\View\Admingroups\view.ctp
    Wrote `E:\work\PHPPro\CakePHPTest\app\View\Admingroups\view.ctp`

    Baking `add` view file...

    Creating file E:\work\PHPPro\CakePHPTest\app\View\Admingroups\add.ctp
    Wrote `E:\work\PHPPro\CakePHPTest\app\View\Admingroups\add.ctp`

    Baking `edit` view file...

    Creating file E:\work\PHPPro\CakePHPTest\app\View\Admingroups\edit.ctp
    Wrote `E:\work\PHPPro\CakePHPTest\app\View\Admingroups\edit.ctp`

    Bake All complete

    4)关于.ctp模板文件
    如果使用zend studio是无法打开.ctp文件的,那么我就增加后缀名吧
    window -> Preferences -> General -> Content Types -> PHP Content type  添加*.ctp后缀
    添加文件关联
    window -> Preferences -> General -> Editor -> File Associations 添加关联

你可能感兴趣的:(cakephp bake)