public function comment($id) { $model = new Model('Article'); $article = $model->find(array('articleId' => $id)); if (empty($article)) { $this->error('文章不存在'); } $key = get_client_ip() . '-view-article-' . $id; $cache = S($key); if (!empty($cache)) { $this->error('评论间隔必须大于1分钟'); } $nickname = I('nickname'); $content = I('content'); if (empty($nickname)) { $this->error('昵称不能为空'); } if (empty($content)) { $this->error('评论内容不能为空'); } $data = array('nickname' => $nickname, 'content' => $content, 'createdAt' => time(), 'createdIp' => get_client_ip(), 'articleId' => $id); $commentModel = new Model('Comment'); if (!$commentModel->data($data)->add()) { $this->error('评论失败'); } S($key, 1, 60); $data['createdAt'] = date('m-d H:i', $data['createdAt']); $this->ajaxReturn($data); }
public function add() { if (IS_POST) { $model = new Model('Category'); $name = I('name'); $isNav = I('isNav', 1); $sort = I('sort', 0); if (empty($name)) { $this->error('请输入分类名称'); } $isExists = $model->where(array('name' => $name))->find(); if (!empty($isExists)) { $this->error('分类已存在'); } $data = array('name' => $name, 'isNav' => $isNav, 'sort' => $sort); if (!$model->data($data)->add()) { $this->error('添加失败'); } $this->success('添加成功', U('admin/category/index')); } else { $this->display('post'); } }
public function add() { if (IS_POST) { $model = new Model('Link'); $name = I('name'); $link = I('link'); $status = I('status', 1); $sort = I('sort', 0); if (empty($name)) { $this->error('网站名称不能为空'); } if (empty($link)) { $this->error('网站链接不能为空'); } $data = array('name' => $name, 'link' => $link, 'status' => $status, 'sort' => $sort); if (!$model->data($data)->add()) { $this->error('添加失败'); } else { $this->success('添加成功', U('admin/link/index')); } } else { $this->display('post'); } }
public function add() { if (IS_POST) { $model = new Model('Article'); $title = I('title'); $description = I('description'); $content = I('content'); $image = I('image'); $status = I('status', 1); $sort = I('sort', 0); $createdAt = time(); $categoryId = I('categoryId'); if (empty($title)) { $this->error('文章标题不能为空'); } if (empty($content)) { $this->error('文章内容不能为空'); } if (empty($description)) { $description = trim(mb_substr(strip_tags(htmlspecialchars_decode($content)), 0, 100, 'UTF-8')); } if (empty($categoryId)) { $this->error('请选择分类'); } $data = array('title' => $title, 'description' => $description, 'content' => $content, 'image' => $image, 'status' => $status, 'sort' => $sort, 'createdAt' => $createdAt, 'categoryId' => $categoryId); if (!$model->data($data)->add()) { $this->error('添加失败'); } M('Category')->where(array('categoryId' => $categoryId))->setInc('total'); $this->success('添加成功', U('admin/article/index')); } else { $categories = M('Category')->field('categoryId,name')->select(); $this->assign('categories', $categories); $this->display('post'); } }
/** * 添加用户 * @param $user * @return bool */ public function addUser($user) { $User = new Model('user'); return $User->data($user)->add(); }
/** * 添加文章 * @param $article */ public function addArticle($article) { $Article = new Model('article'); return $Article->data($article)->add(); }
public function testMagicMethods() { $model = new Model('user', $this->getConfig()); $model->data("first=a&last=z"); $model->username = '******'; $data = ['first' => 'a', 'last' => 'z', 'username' => 'test']; $this->assertEquals($data, $model->data()); $this->assertEquals('a', $model->first); $this->assertTrue(isset($model->last)); unset($model->username); $this->assertTrue(!isset($model->username)); $this->assertEquals(2, $model->count()); $info = $model->getByUsername('test'); $this->assertEquals(1, $info['id']); $id = $model->getFieldByUsername('test', 'id'); $this->assertEquals(1, $id); $id = $model->getFieldByUsername('test', 'id'); $this->assertEquals(1, $id); }