Skip to main content

在CI框架里面写了个简单Mongodb类,实现了增、删、查、改。完整实例代码.

[TOC]

在CI框架里面写了个简单Mongodb类,实现了增、删、查、改。完整实例代码.

现在是学习摸索阶段以后会陆续更新

模型类

<?php if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );


class CI_Mongodb {
var $mongo;
var $db;
function __construct() {
// 连接mongodb
$this->mongo = new Mongo ();
// 选择数据库(不存在会自动创建一个)
$this->db = $this->mongo->xiaoshuo;
}

// mogodb 插入
public function insert($table, $data) {
$collection = $this->db->$table;
$collection->insert ( $data );
}
// mogodb 查询
public function select($table, $where = array()) {
if (isset( $where ['_id'] )) {
$where ['_id'] = new MongoId ( $where ['_id'] );
}
$collection = $this->db->$table;
return $result = $collection->find ($where);
}
// mogodb 查询一行数据
public function row($table, $where = array()) {
if (isset( $where ['_id'] )) {
if (is_array($where ['_id'])) {
;
}else {
$where ['_id'] = new MongoId ( $where ['_id'] );
}
}
$collection = $this->db->$table;
return $result = $collection->findOne ($where);
}
// mogodb 删除
public function delete($table, $where = array()) {
if (isset( $where ['_id'] )) {
$where ['_id'] = new MongoId ( $where ['_id'] );
}
$collection = $this->db->$table;
return $result = $collection->remove ($where);
}
// mogodb 修改
public function update($table,$source ,$where = array()) {
if (isset( $where ['_id'] )) {
$where ['_id'] = new MongoId ( $where ['_id'] );
}
$collection = $this->db->$table;
return $collection->update($where,$source);
}


}

控制器类

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
var $mongo;
var $page_title = '添加新书';

function __construct()
{
parent::__construct();
$this->load->library('Mongodb');
$this->mongo = new CI_Mongodb();
}
public function index()
{
//查询书目录
$where = array();
$result = $this->mongo->select('book',$where);
$book_list = array();
foreach ($result as $obj) {
$book_list[] = $obj ;
}
$data['book_list'] = $book_list ;
$data['page_title'] = $this->page_title;
$data['action'] = site_url('welcome/add');
$this->load->view('book',$data);
}
public function add()
{
if (! empty ( $_POST ['book_name'] )) {
$book_name = trim ( $_POST ['book_name'] );
$author = trim ( $_POST ['author'] );

// 查询书名是否重复
$where = array ("book_name" => $book_name);
$result = $this->mongo->row ( 'book', $where );

if (empty ( $result )) {
// 添加记录
$obj = array (
"book_name" => $book_name,
"author" => $author,
"datetime" => date ( 'Y-m-d H:i:s', time () )
);
$this->mongo->insert ( 'book', $obj );
}else {
echo '此书名已存在';
return ;
}
}

//查询书目录
$where = array();
$result = $this->mongo->select('book',$where);
$book_list = array();
foreach ($result as $obj) {
$book_list[] = $obj ;
}
$data['book_list'] = $book_list ;
$data['page_title'] = $this->page_title;
$data['action'] = site_url('welcome/add');
$this->load->view('book',$data);
}

public function edit($id)
{

//查询书目录
$where = array();
$result = $this->mongo->select('book',$where);
$book_list = array();
foreach ($result as $obj) {
$book_list[] = $obj ;
}
$data['book_list'] = $book_list ;
if (!empty($id))
{
$where = array ('_id' => $id );
$book = $this->mongo->row ( 'book', $where );
}
$data['page_title'] = '修改此书';
$data['book_info'] = $book;
$data['action'] = site_url('welcome/update/'.$id);
$this->load->view('book',$data);
}

public function update($id) {
if (! empty ( $id )) {
if (! empty ( $_POST ['book_name'] )) {
$book_name = trim ( $_POST ['book_name'] );
$author = trim ( $_POST ['author'] );

// 查询书名是否重复
$where = array (
"book_name" => $book_name ,
"_id" =>array('$ne'=> new MongoId($id))
);
$result = $this->mongo->row ( 'book', $where );

if (empty ( $result )) {
// 添加记录
$obj = array (
"book_name" => $book_name,
"author" => $author,
"datetime" => date ( 'Y-m-d H:i:s', time () )
);
$where = array ('_id' => $id );
$source = $this->mongo->row ( 'book', $where );
$result = $this->mongo->update ( 'book', $obj, $where );
} else {
echo '书名重复,修改失败';
return;
}
}
}
header("Location:".site_url('welcome/index'));
}

public function delete($id)
{
if (!empty($id))
{
$where = array ('_id' => $id );
$this->mongo->delete ( 'book', $where );
}

header("Location:".site_url('welcome/index'));
}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

视图类

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>添加书</title>

<style type="text/css">

::selection{ background-color: #E13300; color: white; }
::moz-selection{ background-color: #E13300; color: white; }
::webkit-selection{ background-color: #E13300; color: white; }

body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}

a {
color: #003399;
background-color: transparent;
font-weight: normal;
}

h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}

code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}

#body{
margin: 0 15px 0 15px;
}

p.footer{
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}

#container{
margin: 10px;
border: 1px solid #D0D0D0;
-webkit-box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>

<div id="container">

<table border="1">
<tr>
<td width="200px">ID</td>
<td width="200px">书名</td>
<td width="100px">作者</td>
<td width="160px">时间</td>
<td width="100px">管理</td>
</tr>
<?php foreach($book_list as $book){?>
<tr>
<td><?php echo $book['_id'];?></td>
<td><?php echo $book['book_name'];?></td>
<td><?php echo $book['author'];?></td>
<td><?php echo $book['datetime'];?></td>
<td>
<a href="<?php echo site_url('welcome/edit/'. $book['_id']);?>">修改</a>
<a href="<?php echo site_url('welcome/delete/'. $book['_id']);?>">删除</a>
</td>
</tr>
<?php }?>

</table>


<h1><?php echo $page_title;?></h1>

<div id="body">
<form name="form1" method="post" action="<?php echo $action;?>">
<p>书名:
<label for="textfield"></label>
<input type="text" name="book_name" id="book_name" value="<?php echo empty($book_info['book_name']) ? '' : $book_info['book_name'] ;?>">
</p>
<p>作者:
<input type="text" name="author" id="author" value="<?php echo empty($book_info['author']) ? '' : $book_info['author'] ;?>">
</p>
<p>
<input type="submit" name="button" id="button" value="提交">
</p>
</form>
<p>&nbsp;</p>
</div>

<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>

</body>
</html>