Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14098 anikendra 1
<?php
2
/**
3
 * PostsController class
4
 *
5
 * @uses          AppController
6
 * @package       mongodb
7
 * @subpackage    mongodb.samples.controllers
8
 */
9
class PostsController extends AppController {
10
 
11
 
12
/**
13
 * name property
14
 *
15
 * @var string 'Posts'
16
 * @access public
17
 */
18
	public $name = 'Posts';
19
 
20
/**
21
 * index method
22
 *
23
 * @return void
24
 * @access public
25
 */
26
	public function index() {
27
		$params = array(
28
			'fields' => array('title', 'body', 'hoge'),
29
			//'fields' => array('Post.title', ),
30
			//'conditions' => array('title' => 'hehe'),
31
			//'conditions' => array('hoge' => array('$gt' => '10', '$lt' => '34')),
32
			//'order' => array('title' => 1, 'body' => 1),
33
			'order' => array('_id' => -1),
34
			'limit' => 35,
35
			'page' => 1,
36
		);
37
		$results = $this->Post->find('all', $params);
38
		//$result = $this->Post->find('count', $params);
39
		$this->set(compact('results'));
40
	}
41
 
42
/**
43
 * add method
44
 *
45
 * @return void
46
 * @access public
47
 */
48
	public function add() {
49
		if (!empty($this->data)) {
50
 
51
			$this->Post->create();
52
			if ($this->Post->save($this->data)) {
53
				$this->flash(__('Post saved.', true), array('action' => 'index'));
54
			} else {
55
			}
56
		}
57
	}
58
 
59
/**
60
 * edit method
61
 *
62
 * @param mixed $id null
63
 * @return void
64
 * @access public
65
 */
66
	public function edit($id = null) {
67
		if (!$id && empty($this->data)) {
68
			$this->flash(__('Invalid Post', true), array('action' => 'index'));
69
		}
70
		if (!empty($this->data)) {
71
			if ($this->Post->save($this->data)) {
72
				$this->flash(__('The Post has been saved.', true), array('action' => 'index'));
73
			} else {
74
			}
75
		}
76
		if (empty($this->data)) {
77
			$this->data = $this->Post->read(null, $id);
78
			//$this->data = $this->Post->find('first', array('conditions' => array('_id' => $id)));
79
		}
80
	}
81
 
82
/**
83
 * delete method
84
 *
85
 * @param mixed $id null
86
 * @return void
87
 * @access public
88
 */
89
	public function delete($id = null) {
90
		if (!$id) {
91
			$this->flash(__('Invalid Post', true), array('action' => 'index'));
92
		}
93
		if ($this->Post->delete($id)) {
94
			$this->flash(__('Post deleted', true), array('action' => 'index'));
95
		} else {
96
			$this->flash(__('Post deleted Fail', true), array('action' => 'index'));
97
		}
98
	}
99
 
100
/**
101
 * deleteall method
102
 *
103
 * @return void
104
 * @access public
105
 */
106
	public function deleteall() {
107
		$conditions = array('title' => 'aa');
108
		if ($this->Post->deleteAll($conditions)) {
109
			$this->flash(__('Post deleteAll success', true), array('action' => 'index'));
110
 
111
		} else {
112
			$this->flash(__('Post deleteAll Fail', true), array('action' => 'index'));
113
		}
114
	}
115
 
116
/**
117
 * updateall method
118
 *
119
 * @return void
120
 * @access public
121
 */
122
	public function updateall() {
123
		$conditions = array('title' => 'ichi2' );
124
 
125
		$field = array('title' => 'ichi' );
126
 
127
		if ($this->Post->updateAll($field, $conditions)) {
128
			$this->flash(__('Post updateAll success', true), array('action' => 'index'));
129
 
130
		} else {
131
			$this->flash(__('Post updateAll Fail', true), array('action' => 'index'));
132
		}
133
	}
134
 
135
	public function createindex() {
136
		$mongo = ConnectionManager::getDataSource($this->Post->useDbConfig);
137
		$mongo->ensureIndex($this->Post, array('title' => 1));
138
 
139
	}
140
}