-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
266 lines (160 loc) · 4.64 KB
/
example.php
File metadata and controls
266 lines (160 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
//Example Script, saves Hello World to the database.
//First, we need to include redbean
require_once('rb.php');
class RedBean_MyCustomModel extends RedBean_SimpleModel {
private static $errors = array();
public function error($type, $field, $text) {
self::$errors[$type] = array($field => $text);
}
public function getErrors() {
return self::$errors;
}
public function update() {
//$this->errors = array(); // reset the errors array
}
public function after_update() {
if (count(self::$errors) > 0) {
//throw new Exception('Validation failed');
}
}
}
class Model_Category extends RedBean_MyCustomModel {
private $t = 'category';
public function update() {
parent::update();
if (empty($this->name))
$this->error($this->t, 'name', 'field is empty');
}
}
class Model_Page extends RedBean_MyCustomModel {
private $t = 'page';
public function update() {
parent::update();
if (empty($this->name))
$this->error($this->t, 'name', 'field is empty');
}
}
class Model_Image extends RedBean_MyCustomModel {
private $t = 'image';
public function update() {
parent::update();
if (empty($this->name))
$this->error($this->t, 'name', 'field is empty');
}
}
R::setup('sqlite:/test.db');
R::debug(false);
//R::setup('database.txt'); -- for other systems
$all = R::findAll('page');
$all = R::$f->begin()->select('*')
->from('page')
->left_join('category')
->on('page.id = category.page_id')
->left_join('image')
->on('image.category_id = category.id');
// $test = R::exportAll($all);
echo '<pre>';
print_r($all->get());
//ReflectionObject::export($all);
echo '</pre>';
die();
foreach ($all->get() as $one) {
echo $one->name . '<br>';
foreach ($one->ownCategory as $o)
echo $o->name . '<br>';
}
if (count($_POST)) {
$test = R::graph($_POST);
$err = array();
$page = $test['page'];
$category = $test['category'];
$image = $test['image'];
$category->ownImage[] = $image;
$page->ownCategory[] = $category;
R::begin();
foreach ($test as $t) {
$id = R::store($t);
if (count($t->getErrors()) > 0)
$err[$t->getMeta('type')] = $t->getErrors();
}
$id = R::store($page);
if (count($page->getErrors()) > 0)
$err[$page->getMeta('type')] = $page->getErrors();
if (count($err) > 0)
R::rollback();
else
R::commit();
}
?>
<form method=post action="">
<input type='hidden'name="page[type]" value="page"/>
<input type='hidden'name="category[type]" value="category"/>
<input type='hidden'name="image[type]" value="image"/>
<input type='text'name="page[name]" value=""/>
<input type='text'name="category[name]" value=""/>
<input type='text'name="image[name]" value=""/>
<br>
<input type="submit"/></form>
<?php
IF (count($_POST)) {
echo '<pre>';
print_r($err);
echo '</pre>';
}
/*
class Model_Todo extends RedBean_MyCustomModel {
public function getList() {
return R::findAndExport("todo");
}
}
$beancan = new RedBean_BeanCan;
if (isset($_POST["json"])) die($beancan->handleJSONRequest( $_POST["json"] ));
?>
<html>
<head><title>DEMO BEANCAN</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<h1>My Todo List</h1>
<ul class="list">
<?php foreach(R::find("todo") as $todo): ?>
<li><button todo="<?php echo $todo->id; ?>">DONE</button><?php echo $todo->description; ?></li>
<?php endforeach; ?>
</ul>
<fieldset>
<label>Todo:<label>
<input type="text" id="dscr" />
<button>add to list</button>
</fieldset>
<script>
$("document").ready(function(){
//Delete an Item
var requestDelete = '{"jsonrpc":"2.0","id":"delete","method":"todo:trash","params":[@]}';
var clickHandler = function(){
var $btn = $(this);
$.post("?",{"json":requestDelete.replace("@",$btn.attr("todo"))},function(d){
eval("data="+d);
if (data.id=="delete") $btn.parent().remove();
});
}
//Attach Click Handlers
$("li button").click(clickHandler);
//Add an Item
$("fieldset button").click(function(){
var requestAdd = '{"jsonrpc":"2.0","id":"add_todo","method":"todo:store","params":[{"description":"@"}]}';
$.post("?",{"json":requestAdd.replace("@",$("#dscr").val())},function(d){
data = JSON.parse(d);
if (data.id=="add_todo") {
$(".list").append("<li><button todo='"+data.result+"'>DONE</button>"+$("#dscr").val()+"</li>");
//Restore Click Handlers
$("li button").unbind().click(clickHandler);
}
});
});
});
</script>
</body>
</html>
* *?
*/