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 |
<?php
/*
* Created on 2008 Apr 30
* by Martin Wernstahl <m4rw3r@gmail.com>
*/
/**
* Customizeable print_r variant.
*
* Use only the $subject and $ignore parameters, the others are used internally
*
* @param $subject The object/variable/array to be printed
* @param $ignore An array with the keys to ignore
* @param $depth Current depth (used in recursion)
* @param $refChain A chain of printed objects to avoid recursion
*/
function u_print_r($subject, $ignore = array(), $depth = 1, $refChain = array()){
$skip = false; // print skip?
if ($depth > 20) return;
if (is_object($subject)) {
foreach ($refChain as $refVal)
if (serialize($refVal) == serialize($subject)) {
echo "*RECURSION*\n";
return;
}
array_push($refChain, $subject);
echo get_class($subject) . " Object ( \n";
$subject = (array) $subject;
foreach ($subject as $key => $val)
if (is_array($ignore) && !in_array($key, $ignore, 1)) {
echo str_repeat(" ", $depth * 4) . '[';
if ($key{0} == "\0") {
$keyParts = explode("\0", $key);
echo $keyParts[2] . (($keyParts[1] == '*') ? ':protected' : ':private');
} else
echo $key;
echo '] => ';
u_print_r($val, $ignore, $depth + 1, $refChain);
}
else{
if($skip){
// echoes skip when it skips an ignored value
echo str_repeat(" ", $depth * 4)."[".$key."]: SKIP\n";
}
}
echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
array_pop($refChain);
} elseif (is_array($subject)) {
echo "Array ( \n";
foreach ($subject as $key => $val){
if (is_array($ignore) && !in_array($key, $ignore, 1)) {
echo str_repeat(" ", $depth * 4) . '[' . $key . '] => ';
u_print_r($val, $ignore, $depth + 1, $refChain);
}
else{
if($skip){
// echoes skip when it skips an ignored value
echo str_repeat(" ", $depth * 4)."[".$key."]: SKIP\n";
}
}
}
echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
} else
var_dump($subject) . "\n";
}
function u($subject,$ignore = array()){
echo '<pre>';
u_print_r($subject,$ignore);
echo '</pre>';
}
class irtest extends Controller{
function irtest(){
parent::Controller();
$this->load->database();
$this->load->library('unit_test');
//$this->unit->use_strict(TRUE);
$this->output->enable_profiler(TRUE);
}
// --------------------------------------------------------------------
/**
*
*/
function validation()
{
if(!class_exists('IgnitedRecord')){
// change this line if the IgnitedRecord file is stored elsewhere
require_once(APPPATH.'models/IgnitedRecord/ignitedrecord.php');
}
$this->user = IgnitedRecord::factory('users', array('act_as' => array(
'validation' => array(
'name' => array('rules' => 'trim|required|max_length[10]'),
'password' => array('rules' => 'required|min_length[3]', 'label' => 'Secret')
)
)));
$u = $this->user->new_record();
$u->name = 'lol';
$u->password = 'ed';
$u->validate();
var_dump($this->user->errors());
}
// --------------------------------------------------------------------
/**
*
*/
function performance()
{
define('IR_USE_IQ',true);
if(!class_exists('IgnitedRecord')){
// change this line if the IgnitedRecord file is stored elsewhere
require_once(APPPATH.'models/IgnitedRecord/ignitedrecord.php');
}
$this->benchmark->mark('IR_start');
$this->user = IgnitedRecord::factory('users');
$this->user->habtm('groups');
for($i = 0; $i < 1000; $i++)
{
$o = $this->user->find(1);
$r = $o->related('groups')->order_by('name', 'desc')->get();
}
$this->benchmark->mark('IR_end');
/*$this->benchmark->mark('IR_start');
$this->user = IgnitedRecord::factory('users');
$this->user->habtm('groups');
$this->group = IgnitedRecord::factory('groups');
for($i = 0; $i < 10000; $i++)
{
$o = $this->user->find(1);
$r = $this->group->find_all_by_sql('SELECT * FROM `groups` WHERE `id` IN (SELECT `group_id` FROM `groups_users` WHERE `user_id` = "'.$o->id.'") ORDER BY `name` desc');
}
$this->benchmark->mark('IR_end');*/
/*$this->benchmark->mark('AR_start');
for($i = 0; $i < 1000; $i++){
$o = $this->db->get_where('users',array('id' => 1))->row();
$ids = $this->db->select('group_id')->from('groups_users')->where('user_id', $o->id)->get();
foreach($ids->result() as $id)
{
$tmp[] = $id->group_id;
}
$r = $this->db->where_in('id', $tmp)->from('groups')->order_by('name', 'desc')->get()->result();
}
$this->benchmark->mark('AR_end');*/
/* $this->benchmark->mark('AR_start');
for($i = 0; $i < 10000; $i++){
$o = $this->db->get_where('users',array('id' => 1))->row();
$r = $this->db->query('SELECT * FROM `groups` WHERE `id` IN (SELECT `group_id` FROM `groups_users` WHERE `user_id` = "'.$o->id.'") ORDER BY `name` desc')->result();
}*
$this->benchmark->mark('AR_end');*/
/*
$this->benchmark->mark('AR_start');
for($i = 0; $i < 10000; $i++)
{
$o = $this->db->get_where('users',array('id' => 1))->result();
}
$this->benchmark->mark('AR_end');*/
}
// --------------------------------------------------------------------
/**
*
*/
function bla()
{
define('IR_USE_IQ',true);
if(!class_exists('IgnitedRecord')){
// change this line if the IgnitedRecord file is stored elsewhere
require_once(APPPATH.'models/IgnitedRecord/ignitedrecord.php');
}
$this->user = IgnitedRecord::factory('users');
$this->user->habtm('groups');
/*$this->ticket = IgnitedRecord::factory('tickets');
$this->ticket->has_many('answers');
$this->ticket->belongs_to('user');
$this->answer = IgnitedRecord::factory('answers');
$this->answer->belongs_to('answer');*/
/*var_dump(isset($l));
$l = array();
var_dump(isset($l));
$this->load->orm();
$this->user = IgnitedRecord::factory('users',array('has_many' => 'tickets'));
$this->ticket = IgnitedRecord::factory('tickets',array('belongs_to' => 'users','has_many' => 'answers'));
$this->answer = IgnitedRecord::factory('answers',array('belongs_to' => 'tickets'));*/
$this->user->join_related('groups');
$user = $this->user->find_all();
/*u($user);
echo $this->db->last_query();
/*foreach($user->related('tickets')->get() as $ticket)
{
u($ticket,array('__instance','__relations','__relation_properties','__data','mpttree','tree'));
foreach($ticket->get_related('answers') as $answer)
{
echo $answer->text;
}
}*/
}
/**
* Test suite for IgnitedRecord.
*/
function index(){
// set up test tables:
/*$this->db->query(
'DROP TABLE IF EXISTS `test_posts`;
CREATE TABLE "test_posts" (
"id" int(10) unsigned NOT NULL auto_increment,
"text" varchar(300) NOT NULL,
"test_users_id" int(10) unsigned NOT NULL,
PRIMARY KEY ("id")
) AUTO_INCREMENT=1 ;');
$this->db->query(
'DROP TABLE IF EXISTS `test_users`;
CREATE TABLE "test_users" (
"id" int(10) unsigned NOT NULL auto_increment,
"name" varchar(200) NOT NULL,
"email" varchar(200) NOT NULL,
PRIMARY KEY ("id")
) AUTO_INCREMENT=1 ;');*/
// start test
$this->load->orm();
$this->unit->run(class_exists('IgnitedRecord'),
true,'Loading of Ignitedrecord');
$this->user = IgnitedRecord::yamlfactory(
'table: test_users
habtm:
-name: lol
table: test_habtm
model: blaaaa
');
$this->unit->run(isset($this->user) && $this->user instanceof IgnitedRecord,
true,'Creating a model with the yamlfactory() method');
$this->unit->run($this->user->table,
'test_users','Checking __table is correctly set');
$this->post = IgnitedRecord::factory('test_posts',
array('belongs_to' => 'test_users',
'habtm' => array('name' => 'lol','table' => 'test_habtm','model' => 'user')
));
$this->unit->run(isset($this->post) && $this->post instanceof IgnitedRecord,
true,'Creating a model with the factory() method');
$this->unit->run($this->post->table,
'test_posts','Checking __table is correctly set');
echo $this->unit->summary_report();
}
}
?> |