-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_simpleorm.php
61 lines (46 loc) · 1.74 KB
/
test_simpleorm.php
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
<?php
/* include autoload */
require_once(__DIR__ . '/library/Airaghi/Tools/AutoLoad.php' );
\Airaghi\Tools\AutoLoad::enableAutoLoad();
/* connect to mysql db */
$test = \Airaghi\DB\SimpleORM\Adapter::create('mysql',array('database'=>'test'));
/* model describing a table */
class Person extends \Airaghi\DB\SimpleORM\Model {
public $id;
public $email;
public $nome;
public $data;
}
/* create a query extracting only distinct records with email not empty and id equal to some value */
$query = Person::initQuery();
$query->setDistinct()->setWhere()->appendCondition('id','=','?')->appendAnd()->appendOpenBlock()->appendCondition('email','<>','""')->appendCloseBlock()->close();
$list = Person::find($query,array(1));
if ($list) {
foreach ($list as $x) {
print_r($x);
}
}
$list->release();
/* create a query extracting only records with email not empty and id equal to some value */
$query = Person::initQuery();
$query->setWhere()->appendCondition('id','=','?')->appendAnd()->appendOpenBlock()->appendCondition('email','<>','""')->appendCloseBlock()->close();
/* count records matching the condition defined before and id=3 */
$tot = Person::getCount($query,array(3));
/* get the list or records matching the condition defined before and id=3 */
$list = Person::find($query,array(1));
if ($list) {
foreach ($list as $l) {
// print element
print_r($l);
}
}
$list->release();
/* get only the first record matching the condition defined before and id=3 */
$element = Person::findFirst($query,array(3));
/* delete every record with id>1 */
$query = Person::initQuery();
$query->setWhere()->appendCondition('id','>','?');
Person::deleteBatch($query,array(1),array(\Airaghi\DB\SimpleORM\Adapter::TYPE_INTEGER));
/* close db connection */
$test->close();
?>