1目的
2代码规范要求
3程序设计原则
3.1Model层
所有与数据库表本身操作方法,都封装在自己的Model类中,不单独在外提供封装。
每个Model基于ThinkPHP都已经封装好了一套DB操作方法。
3.1.1基类
3.1.1.1Model
ThinkPHP原生Model基类。不能修改。
在基于单表的数据CURD操作,使用Model原生成的功能。如:
3.1.1.1.1Insert
$role_user_model->add(array("role_id" =>$role_id, "user_id" =>$uid, "oem_code" => parent::oem_code()));
3.1.1.1.2Update
if (IS_POST) {
$_POST['id'] = get_current_admin_id();
$_POST["oem_code"] = parent::oem_code();
$create_result =
$this->users_model->field("user_login,user_email,last_login_ip,last_login_time,create_time,user_ activation_key,user_status,role_id,score,user_type,oem_code", true)//排除相关字段
->create();
if ($create_result) {
if ($this->users_model->save() !== false) {
$this->success("保存成功!");
} else {
$this->error("保存失败!");
}
} else {
$this->error($this->users_model->getError());
}
}
3.1.1.1.3Delete
$id = intval(I("get.id"));
if ($this->users_model->where("id=$id")->delete() !== false) {
M("RoleUser")->where(array("user_id" =>$id, "oem_code" =>
parent::oem_code()))->delete();
$this->success("删除成功!");
} else {
$this->error("删除失败!");
}
3.1.1.1.4Where
$id = get_current_admin_id();
$user = $this->users_model->where(array("id" =>$id, "oem_code" =>
parent::oem_code()))->find();
3.1.1.1.5Page
ThinkPHP已经实现好了分页栏位计算方法。
HTML中
PHP中
$count = $this->users_model->where($condition_user)->count();
$page = $this->page($count, 20);
$users = $this->users_model
->where($condition_user)// 加了过滤
->order("create_time DESC")
->limit($page->firstRow . ',' . $page->listRows)
->select();
$this->assign("page_bar", $page->show('Admin')); // Admin 是模板名称,默认可以不填$this->assign("users", $users);
3.1.1.2CommonModel: Model
通用Model。构造时会所用默认的数据库连接。
3.1.1.3OemModel: CommonModel: Model
针对不同OEM需要访问自己的DB时所使用的Model。重写了构造函数,初始化时会使用OEM定义好的数据库连接信息。
3.1.2通用设置
3.1.2.1自动后台验证
3.1.2.2默认值设定
3.1.3通用方法
目前Model中可以通用的方法都写在CommonModel中。个人尽量不要在些文件中定义自己的函数,如果确认有的方法很常用、很通用,提交项目组进行讨论并确认后,方可写入CommonModel文件中。
ThinkPHP提供了的原生数据库表操作功能,非常基础的Table操作可以直接写在上层代码中,如Controller中可以直接进行调用。
原生的快速数据库操作方法:
3.1.3.1Select top 10 * from Table where condition
$list = $this->where($condition)->limit(10)->select();
3.1.3.2Select a as aa, b, c from table where condition
$list = $this
->where($arrayCondition)
->field(array(“a”=>”aa”, “b”, “c”))
->select();
return $list;
3.1.3.3Insert table (a, b, c) values (a, b, c)
普通做法:
$data['create_time'] = date("Y-m-d H:i:s", time());
$data['code'] = uniqid();
$result = $this->add($data);
使用ThinkPHP直接从POST中将Form数据存库方法:
if (IS_POST) {
$_POST["oem_code"] = 100;
if ($this->menu_model->create()) {
if ($this->menu_model->save() !== false) {
…
3.1.3.4Update table set a = @a where id = @id
$data['id'] = 100;
$data['update_time'] = date("Y-m-d H:i:s", time());
$result = $this->save($data);
3.1.3.5Delete table where id = @id
$result = $this->where("id=$id")->delete();
目前可以使用的自定义方法如下:
3.1.4自定义方法
所有与自身表数据有关的操作,各自封装在自己的Model文件中。命名规则:
<操作><实体><条件>(….)
?getRegisterUser(….);
?setUserAgeById($id, $age);
?getUsersByCondiation($condition);
?deleteUserByCompanyAndStatus($company, $status);
3.2Business Logic层
没想好放在哪个目录下
3.2.1命名规则
3.2.1.1类名
按业务场景起名
如:
?AlarmLogic
?PurchaseLogic
?CrmLogic
?NotificateLogic
3.2.1.2方法命名规则
动词+名词
如:
?moveAlarmToOtherUser($alarmid, $userid);
?copyNotificationToCompany($notificateid, $companyid);
?deleteExpireUserByCompanyPolicy($companyid);
3.3Common Function层
路径:Application/Common/Common/Function.php下。
全局功能性的函数,或者全局通用的方法都写在这里。
目前定义的函数如下
3.3.1iot_oem_code()
取当前用户所属的oem_code。
3.4Session的使用
3.4.1$_SESSION*“user”+
4表单处理
4.1列表页面
4.1.1文件名定义
?index
?list
?home
?search
4.2添加页面
4.2.1文件名定义
?add
?create
4.2.2后台Action名
?add_post
?create_post