1
|
|
|
<?php |
2
|
|
|
namespace PhpBoot\ORM; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Cache\Cache; |
5
|
|
|
use PhpBoot\DB\DB; |
6
|
|
|
|
7
|
|
|
class ModelWithClass |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Model constructor. |
11
|
|
|
* @param DB $db |
12
|
|
|
* @param string $entityName |
13
|
|
|
* @param Cache $cache |
14
|
|
|
*/ |
15
|
7 |
|
public function __construct(DB $db, $entityName, Cache $cache) |
|
|
|
|
16
|
|
|
{ |
17
|
7 |
|
$this->db = $db; |
18
|
7 |
|
$builder = $db->getApp()->get(ModelContainerBuilder::class); |
19
|
7 |
|
$this->entity = $builder->build($entityName); |
20
|
7 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $id |
24
|
|
|
* @return mixed|null |
25
|
|
|
*/ |
26
|
1 |
|
public function find($id) |
27
|
|
|
{ |
28
|
1 |
|
$row = $this->db->select($this->getColumns()) |
|
|
|
|
29
|
1 |
|
->from($this->entity->getTable()) |
30
|
1 |
|
->where("`{$this->entity->getPK()}` = ?", $id) |
31
|
1 |
|
->getFirst(); |
32
|
1 |
|
if($row){ |
33
|
|
|
return $this->entity->make($row, false); |
34
|
|
|
}else{ |
35
|
1 |
|
return null; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return int rows deleted |
41
|
|
|
*/ |
42
|
1 |
|
public function delete($id) |
43
|
|
|
{ |
44
|
1 |
|
return $this->db->deleteFrom($this->entity->getTable()) |
45
|
1 |
|
->where([$this->entity->getPK()=>$id]) |
46
|
1 |
|
->limit(1) |
47
|
1 |
|
->exec()->rows; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* where 语法见 @see WhereRule |
52
|
|
|
* @param array|string $expr |
|
|
|
|
53
|
|
|
* @param mixed|null $_ |
54
|
|
|
* @return \PhpBoot\DB\rules\basic\WhereRule |
55
|
|
|
*/ |
56
|
1 |
|
public function deleteWhere($conditions, $_=null) |
|
|
|
|
57
|
|
|
{ |
58
|
1 |
|
$query = $this->db->deleteFrom($this->entity->getTable()); |
59
|
1 |
|
return call_user_func_array([$query, 'where'], func_get_args()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return false|int |
64
|
|
|
*/ |
65
|
|
|
public function count() |
66
|
|
|
{ |
67
|
|
|
return $this->db->select($this->getColumns()) |
68
|
|
|
->from($this->entity->getTable()) |
69
|
|
|
->count(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* where 语法见 @see WhereRule |
74
|
|
|
* @param array|string|callable|null $conditions |
75
|
|
|
* @param string $_ |
76
|
|
|
* @return \PhpBoot\DB\rules\select\WhereRule |
77
|
|
|
*/ |
78
|
2 |
|
public function findWhere($conditions=null, $_=null) |
|
|
|
|
79
|
|
|
{ |
80
|
2 |
|
$query = $this->db->select($this->getColumns()) |
81
|
2 |
|
->from($this->entity->getTable()); |
82
|
|
|
$query->context->resultHandler = function ($result){ |
83
|
|
|
foreach ($result as &$i){ |
84
|
|
|
$i = $this->entity->make($i, false); |
85
|
|
|
} |
86
|
|
|
return $result; |
87
|
|
|
}; |
88
|
2 |
|
return call_user_func_array([$query, 'where'], func_get_args()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int|string $id |
93
|
|
|
* @param array $values |
94
|
|
|
* @return int updated row count |
95
|
|
|
*/ |
96
|
1 |
|
public function update($id, $values) |
97
|
|
|
{ |
98
|
1 |
|
return $this->db->update($this->entity->getTable()) |
99
|
1 |
|
->set($values) |
100
|
1 |
|
->where([$this->entity->getPK()=>$id]) |
101
|
1 |
|
->exec(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $values |
106
|
|
|
* @param array|string|callable $conditions where 语法见 @see WhereRule |
107
|
|
|
* @param string $_ |
108
|
|
|
* @return \PhpBoot\DB\rules\basic\WhereRule |
109
|
|
|
*/ |
110
|
1 |
|
public function updateWhere($values, $conditions, $_=null) |
|
|
|
|
111
|
|
|
{ |
112
|
1 |
|
$query = $this->db->update($this->entity->getTable())->set($values); |
113
|
1 |
|
return call_user_func_array([$query, 'where'], array_slice(func_get_args(),1)); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
/** |
117
|
|
|
* set entity table name |
118
|
3 |
|
* @param string $tableName |
119
|
3 |
|
* @return $this |
120
|
3 |
|
*/ |
121
|
3 |
|
public function withTable($tableName) |
122
|
3 |
|
{ |
123
|
|
|
$this->entity->setTable($tableName); |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function getColumns() |
128
|
|
|
{ |
129
|
|
|
$columns = []; |
130
|
|
|
foreach ($this->entity->getProperties() as $p){ |
131
|
|
|
$columns[] = $p->name; |
132
|
|
|
} |
133
|
|
|
return $columns; |
134
|
|
|
} |
135
|
|
|
/** |
136
|
|
|
* @var ModelContainer |
137
|
|
|
*/ |
138
|
|
|
protected $entity; |
139
|
|
|
/** |
140
|
|
|
* @var DB |
141
|
|
|
*/ |
142
|
|
|
protected $db; |
143
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.