public function add()
{
if (IS_POST) {
$Channel = D('Channel');
$data = $Channel->create();
if ($data) {
$id = $Channel->add();
if ($id) {
$this->success('新增成功', U('index'));
//记录行为
action_log('update_channel', 'channel', $id, UID);
} else {
$this->error('新增失败');
}
} else {
$this->error($Channel->getError());
}
} else {
$pid = I('get.pid', 0);
//获取父导航
if (!empty($pid)) {
$parent = M('Channel')->where(array('id' => $pid))->field('title')->find();
$this->assign('parent', $parent);
}
$pnav = D('Channel')->where(array('pid' => 0))->select();
$this->assign('pnav', $pnav);
$this->assign('pid', $pid);
$this->assign('info', null);
$this->meta_title = '新增导航';
$this->display('edit');
}
}这是ONETHINK中的一段代码,在一个控制器里面实现的ADD的方法。public function register($username, $nickname, $password, $email='', $mobile='', $type=1)
{
$data = array(
'username' => $username,
'password' => $password,
'email' => $email,
'mobile' => $mobile,
'type' => $type,
);
//验证手机
if (empty($data['mobile'])) unset($data['mobile']);
if (empty($data['username'])) unset($data['username']);
if (empty($data['email'])) unset($data['email']);
/* 添加用户 */
$usercenter_member = $this->create($data);
if ($usercenter_member) {
$result = D('Common/Member')->registerMember($nickname);
if ($result > 0) {
$usercenter_member['id'] = $result;
$uid = $this->add($usercenter_member);
if ($uid === false) {
//如果注册失败,则回去Memeber表删除掉错误的记录
D('Common/Member')->where(array('uid' => $result))->delete();
}
action_log('reg','ucenter_member',1,1);
return $uid ? $uid : 0; //0-未知错误,大于0-注册成功
} else {
return $result;
}
} else {
return $this->getError(); //错误详情见自动验证注释
}
}又把添加数据的方法用到了MODEL里面。ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。