forked from wattsjus/plinq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataModel.php
More file actions
executable file
·81 lines (79 loc) · 2.38 KB
/
DataModel.php
File metadata and controls
executable file
·81 lines (79 loc) · 2.38 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
<?php
namespace pLinq;
use Where;
class DataModel {
public static function as($alias = null): AsModel
{
$as1 = new AsModel();
$as1->Table = get_called_class();
$as1->Alias = $alias;
return $as1;
}
public static function join($toJoin, $joinFields, $joinAlias = null): Join
{
$join = new Join();
$join->LeftAs = DataModel::as();
$join->RightAs = $toJoin;
$join->JoinFields = $joinFeilds;
$join->JoinAlias = $joinAlias;
return $join;
}
public static function where($conditions): Where
{
$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 Group();
$as1 = new AsModel();
$as1->Table = get_called_class();
$where->Other = $as1;
return $fields;
}
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 . Select::GetSqlFragment($conditions);
}
$db->query($sql);
}
public static function select($fields): Select
{
return new Select(get_called_class(), $fields);
}
}