tp操作mongodb数据库之简易留言板

来源:互联网 发布:ubuntu ssh root 编辑:程序博客网 时间:2024/05/06 04:05

tp操作mongodb数据库之简易留言板

包含的常规操作:数据增加,数据修改,数据删除。

conf

<?php
return array(
    'db_type' => 'mongo',
    'db_host' => 'localhost',
    'db_port' => '27017',
    'db_Name' => 'test',
);

 

controller

<?php

namespace Notes\Controller;

use Think\Controller;

/**
* 文档: IndexController
* @作者: gongmingshi
* @邮件地址: gongmingshi@qq.com
*/
class IndexController extends Controller {

    public function index() {
        $m = D('notes');
        if (isset($_REQUEST['m'])) {
            $w['_id'] = I('request.m');
            $dd = $m->where($w)->find();
            if (isset($_REQUEST['i'])) {
                $this->assign($dd['replay'][$_REQUEST['i']]);
            } else {
                $this->assign($dd);
            }
            //var_dump($dd);
        }
        $d = $m->select();
        $this->assign("data", $d);
        $this->display();
    }

    public function add() {
        $m = D('notes');
        if (isset($_POST['m'])) {
            $w['_id'] = $_POST['m'];
            if (isset($_POST[i])) {
                $i = I('post.i');
                $d = array("replay." . $i . ".title" => I('post.title'), "replay." . $i . ".content" => I('post.content'));
            } else {
                $d = array("title" => I('post.title'), "content" => I('post.content'));
            }
            $r = $m->where($w)->save($d);
            if ($r) {
                $this->success("回复成功修改", __CONTROLLER__ . "/index");
            }
        } elseif (isset($_POST['replay'])) {
            $w['_id'] = I('post.replay');
            $d["replay"] = array("push", I('post.'));
            $r = $m->where($w)->save($d);
            if ($r) {
                $this->success("回复成功", __CONTROLLER__ . "/index");
            }
        } else {
            if (!0 == count(array_filter(I('post.')))) {
                $d = $m->add($_POST);
                if ($d) {
                    $this->success("成功插入", __CONTROLLER__ . "/index");
                }
            } else {
                $this->error("无数据插入");
            }
        }
    }

    public function mydel() {
        $m = D('notes');
        $w['_id'] = I('request.id');
        if (isset($_REQUEST['i'])) {
            $d["replay"] = array("pop", I('request.i'));
            $r = $m->where($w)->save($d);
            if ($r) {
                $this->success("回复成功删除", __CONTROLLER__ . "/index");
            }
        } else {
            $dd = $m->where($w)->delete();
            if ($dd) {
                $this->success("留言成功删除", __CONTROLLER__ . "/index");
            }
        }
    }
}

 

view:略

0 0