1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\ORM; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use PhpBoot\DB\DB; |
7
|
|
|
|
8
|
|
|
class ModelWithObject |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* ModelWithObject constructor. |
12
|
|
|
* @param DB $db |
13
|
|
|
* @param object $entity |
14
|
|
|
* @param Cache $cache |
15
|
|
|
*/ |
16
|
4 |
|
public function __construct(DB $db, $entity, Cache $cache) |
|
|
|
|
17
|
|
|
{ |
18
|
4 |
|
is_object($entity) or \PhpBoot\abort(new \InvalidArgumentException('object required')); |
|
|
|
|
19
|
4 |
|
$entityName = get_class($entity); |
20
|
4 |
|
$this->db = $db; |
21
|
4 |
|
$builder = $db->getApp()->get(ModelContainerBuilder::class); |
22
|
4 |
|
$this->entity = $builder->build($entityName); |
23
|
4 |
|
$this->object = $entity; |
24
|
4 |
|
} |
25
|
|
|
/** |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
1 |
|
public function create() |
29
|
|
|
{ |
30
|
1 |
|
$data = []; |
31
|
1 |
|
foreach ($this->getColumns() as $column){ |
32
|
1 |
View Code Duplication |
if(isset($this->object->$column)){ |
|
|
|
|
33
|
1 |
|
if(is_array($this->object->$column) || is_object($this->object->$column)){ |
34
|
|
|
$data[$column] = json_encode($this->object->$column); |
35
|
|
|
}else{ |
36
|
1 |
|
$data[$column] = $this->object->$column; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
} |
40
|
1 |
|
} |
41
|
1 |
|
$id = $this->db->insertInto($this->entity->getTable()) |
42
|
1 |
|
->values($data) |
43
|
1 |
|
->exec()->lastInsertId(); |
44
|
1 |
|
$this->object->{$this->entity->getPK()} = $id; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $columns columns to update. if columns is empty array, update all of the columns |
49
|
|
|
* @return int rows updated |
50
|
|
|
*/ |
51
|
2 |
|
public function update(array $columns=[]) |
52
|
|
|
{ |
53
|
2 |
|
$data = []; |
54
|
2 |
|
$pk = $this->entity->getPK(); |
55
|
2 |
|
foreach ($this->getColumns() as $column){ |
56
|
2 |
|
if(count($columns) && !in_array($column, $columns)){ |
57
|
1 |
|
continue; |
58
|
|
|
} |
59
|
2 |
View Code Duplication |
if($pk != $column && isset($this->object->$column)){ |
|
|
|
|
60
|
2 |
|
if(is_array($this->object->$column) || is_object($this->object->$column)){ |
61
|
|
|
$data[$column] = json_encode($this->object->$column); |
62
|
|
|
}else{ |
63
|
2 |
|
$data[$column] = $this->object->$column; |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
2 |
|
} |
67
|
|
|
|
68
|
2 |
|
return $this->db->update($this->entity->getTable()) |
69
|
2 |
|
->set($data) |
70
|
2 |
|
->where("`{$pk}` = ?", $this->object->$pk) |
71
|
2 |
|
->exec()->rows; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return int rows deleted |
76
|
|
|
*/ |
77
|
1 |
|
public function delete() |
78
|
|
|
{ |
79
|
1 |
|
$pk = $this->entity->getPK(); |
80
|
1 |
|
return $this->db->deleteFrom($this->entity->getTable()) |
81
|
1 |
|
->where([$pk => $this->object->$pk]) |
82
|
1 |
|
->exec()->rows; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
/** |
86
|
|
|
* set entity table name |
87
|
3 |
|
* @param string $tableName |
88
|
3 |
|
* @return $this |
89
|
3 |
|
*/ |
90
|
3 |
|
public function withTable($tableName) |
91
|
3 |
|
{ |
92
|
|
|
$this->entity->setTable($tableName); |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function getColumns() |
97
|
|
|
{ |
98
|
|
|
$columns = []; |
99
|
|
|
foreach ($this->entity->getProperties() as $p){ |
100
|
|
|
$columns[] = $p->name; |
101
|
|
|
} |
102
|
|
|
return $columns; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var object |
107
|
|
|
*/ |
108
|
|
|
protected $object; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var ModelContainer |
112
|
|
|
*/ |
113
|
|
|
protected $entity; |
114
|
|
|
/** |
115
|
|
|
* @var DB |
116
|
|
|
*/ |
117
|
|
|
protected $db; |
118
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.