1.设置dataSource
默认情况下,Wheels使用你的app的文件夹名作为dataSourceName(当然这也需要在administrator里配置好),如果你不喜欢你也可以在config/settings.cfm这里面进行设置
<cfset set(dataSourceName="")> dataSourceName是你在administrator自定义的
<cfset set(dataSourceUserName="")> dataSourceUserName是连接DB的name(i.e sa)
<cfset set(dataSourcePassword="")> dataSourcePasswod是连接DB的pass
注意:dataSourceUserName和dataSourcePasswod可以不写
2.建表
CREATE TABLE [dbo].[users](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](100) COLLATE Chinese_PRC_CI_AS NOT NULL,
[email] [varchar](255) COLLATE Chinese_PRC_CI_AS NOT NULL,
[password] [varchar](15) COLLATE Chinese_PRC_CI_AS NOT NULL,
CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
users表包含四个字段(id,name,email,password)其中id为主键自增
3.CRUD
(1)在views/users下新建add.cfm
<cfoutput> <h1>Create a New User</h1> #startFormTag(action="create")# <div>#textField(objectName="user", property="name", label="Name")#</div> <div>#textField(objectName="user", property="email", label="Email")#</div> <div>#passwordField(objectName="user", property="password", label="Password")#</div> <div>#submitTag()#</div> #endFormTag()# </cfoutput>
Wheels自带的标签不用解释,与其它的标签没什么不同,最终都会被翻译成html,我们来看objectName="user"这句就是ROR约定胜于配置的体现,这里的单数的user对应着数据库复数的users表。
接下来我们需要在controllers/users.cfc中建立create function来处理add
<cffunction name="create"> <cfset user = model("user").new()> <cfset model("user").create(params.user)> <cfset flashInsert(success="User #params.user.name# created successfully.")> <cfset redirectTo(action="index")> </cffunction>
这句<cfset user = model("user").new()>创建一个空的user对象用来接受add.cfm出过来的数据,这里以及后面代码中的model("user")对应数据库中的users表
最后一句<cfset redirectTo(action="index")>又有一个action,Ok,我们再写一个function来处理这个请求
<cffunction name="index"> <cfset users = model("user").findAll(order="name")> </cffunction>
接下来我们在views/users建立view index.cfm来显示结果
<cfoutput> <h1>Users</h1> <cfif flashKeyExists("success")> <p class="success">#flash("success")#</p> </cfif> <p>#linkTo(text="+ Add New User", action="add")#</p> <table> <thead> <tr> <th>Name</th> <th>Email</th> <th colspan="2"></th> </tr> </thead> <tbody> <cfloop query="users"> <tr> <td>#users.name#</td> <td>#users.email#</td> <td> #linkTo( text="Edit", action="edit", key=users.id, title="Edit #users.name#" )# </td> <td> #linkTo( text="Delete", action="delete", key=users.id, title="Delete #users.name#", confirm="Are you sure that you want to delete #users.name#?" )# </td> </tr> </cfloop> </tbody> </table> </cfoutput>
至此我们已经完成了C和R
下面我们接着完成U和D
我们看到#linkTo(
text="Edit", action="edit", key=users.id,
title="Edit #users.name#"
)#
这是一个超链接,有一个action="edit" OK,我们在controllers/users.cfc建立edit function来处理这个请求
<cffunction name="edit"> <cfset user = model("user").findByKey(params.key)> </cffunction>
接着在views/users建立edit view来显示要edit的user的信息
<cfoutput> <h1>Edit User #user.name#</h1> <cfif flashKeyExists("success")> <p class="success">#flash("success")#</p> </cfif> #startFormTag(action="update")# <div>#hiddenField(objectName="user", property="id")#</div> <div>#textField(objectName="user", property="name", label="Name")#</div> <div>#textField(objectName="user", property="email", label="Email")#</div> <div> #passwordField(objectName="user", property="password", label="Password")# </div> <div>#submitTag()#</div> #endFormTag()# </cfoutput>
同样点submit按钮有需要一个update function来处理(controllers/users.cfc)
<cffunction name="update"> <cfset user = model("user").findByKey(params.user.id)> <cfset user.update(params.user)> <cfset flashInsert(success="User #user.name# updated successfully.")> <cfset redirectTo(action="index", key=user.id)> </cffunction>
<cfset redirectTo(action="index", key=user.id)>又回到了index.cfm
接着处理index.cfm中的#linkTo(
text="Delete", action="delete", key=users.id,
title="Delete #users.name#",
confirm="Are you sure that you want to delete
#users.name#?"
)#
在controllers/users.cfc新建delete function
<cffunction name="delete"> <cfset user = model("user").findByKey(params.key)> <cfset user.delete()> <cfset flashInsert(success="#user.name# was successfully deleted.")> <cfset redirectTo(action="index")> </cffunction>
搞定啦!