-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata-model.php
More file actions
84 lines (84 loc) · 2.67 KB
/
Copy pathdata-model.php
File metadata and controls
84 lines (84 loc) · 2.67 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
<?php
namespace pLinq;
include_once(__DIR__.'/as.php');
include_once(__DIR__.'/group.php');
include_once(__DIR__.'/having.php');
include_once(__DIR__.'/join.php');
include_once(__DIR__.'/where.php');
include_once(__DIR__.'/select.php');
include_once(__DIR__.'/limit.php');
include_once(__DIR__.'/order.php');
class DataModel {
public static function as($alias = null) {
$as1 = new \pLinq\AsModel();
$as1->Table = get_called_class();
$as1->Alias = $alias;
return $as1;
}
public static function join($toJoin, $joinFields, $joinAlias = null) {
$join = new \pLinq\Join();
$join->LeftAs = DataModel::as();
$join->RightAs = $toJoin;
$join->JoinFields = $joinFeilds;
$join->JoinAlias = $joinAlias;
return $join;
}
public static function where($conditions) {
$where = new Where();
$as1 = new AsModel();
$as1->Table = get_called_class();
$where->Other = $as1;
$where->Conditions = $conditions;
return $where;
}
public static function group($fields) {
$group = new \pLinq\Group();
$as1 = new \pLinq\AsModel();
$as1->Table = get_called_class();
$where->Other = $as1;
$group = $fields;
return $group;
}
public static function insert($data) {
$class = get_called_class();
global $dal;
$db = $dal->getConnection($class);
$fields = array();
$values = array();
foreach($data as $key => $value) {
$fields[] = "`$key`";
$values[] = "'$value'";
}
$fields = implode(',',$fields);
$values = implode(',',$values);
$sql = "INSERT INTO `$class` ($fields) VALUES ($values)";
$db->query($sql);
return $db->insert_id;
}
public static function update($conditions, $data) {
$class = get_called_class();
global $dal;
$db = $dal->getConnection($class);
$sql = "UPDATE `$class` SET ";
$fields = array();
foreach($data as $key => $value) {
$fields[] = "`$key` = '$value'";
}
$sql = $sql . implode(',', $fields);
if(is_string($conditions)) {
$sql = $sql . " WHERE ";
$wheres = array();
foreach($conditions as $key=> $value) {
$wheres[] = "`$key` = '$value'";
}
$sql = $sql . implode(',', $wheres);
} else if($class == 'Where') {
$sql = $sql . \pLinq\Select::GetSqlFragment($conditions);
}
$db->query($sql);
}
public static function select($fields) {
return new \pLinq\Select(get_called_class(), $fields);
}
}
?>