1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 08.01.2018 14:46 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Core; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use \DBAL\DbQuery; |
10
|
|
|
use \DBAL\ActiveRecord; |
11
|
|
|
use Common\Traits\TContainer; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EntityDb |
15
|
|
|
* |
16
|
|
|
* Represents in-game entity which have representation in DB (aka one or more connected ActiveRecords) |
17
|
|
|
* |
18
|
|
|
* @package Core |
19
|
|
|
* |
20
|
|
|
* @method array asArray() Extracts values as array [$propertyName => $propertyValue] (from ActiveRecord) |
21
|
|
|
* @method bool update() Updates DB record(s) in DB (from ActiveRecord) |
22
|
|
|
*/ |
23
|
|
|
class EntityDb extends Entity implements \Common\Interfaces\IContainer { |
24
|
|
|
use TContainer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string $_activeClass |
28
|
|
|
*/ |
29
|
|
|
protected $_activeClass = ''; // \\DBAL\\ActiveRecord |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ActiveRecord $_container |
33
|
|
|
*/ |
34
|
|
|
protected $_container; |
35
|
|
|
|
36
|
|
|
protected $_isNew = true; |
37
|
|
|
protected $_isDeleted = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return ActiveRecord |
41
|
|
|
*/ |
42
|
|
|
public function _getContainer() { |
43
|
|
|
return $this->_container; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* EntityDb constructor. |
48
|
|
|
*/ |
49
|
|
|
public function __construct() { |
50
|
|
|
$this->reset(); |
51
|
|
|
// $this->dbLoadRecord($id); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set flag "for update" |
56
|
|
|
* |
57
|
|
|
* @param bool $forUpdate - DbQuery::DB_FOR_UPDATE | DbQuery::DB_SHARED |
58
|
|
|
*/ |
59
|
|
|
public function setForUpdate($forUpdate = DbQuery::DB_FOR_UPDATE) { |
60
|
|
|
$className = $this->_activeClass; |
61
|
|
|
/** |
62
|
|
|
* @var ActiveRecord $className |
63
|
|
|
*/ |
64
|
|
|
$className::setForUpdate($forUpdate); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param int|float $id |
71
|
|
|
* |
72
|
|
|
* @return static |
73
|
|
|
*/ |
74
|
|
|
public function dbLoadRecord($id) { |
75
|
|
|
$this->reset(); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var ActiveRecord $className |
79
|
|
|
*/ |
80
|
|
|
$className = $this->_activeClass; |
81
|
|
|
$container = $className::findById($id); |
82
|
|
|
if(!empty($container)) { |
83
|
|
|
$this->_isNew = false; |
84
|
|
|
$this->_container = $container; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
*/ |
93
|
|
|
public function dbUpdate() { |
94
|
|
|
$this->_getContainer()->update(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
public function isNew() { |
99
|
|
|
return $this->_isNew; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function isDeleted() { |
103
|
|
|
return $this->_isDeleted; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function reset() { |
107
|
|
|
// unset($this->_container); |
|
|
|
|
108
|
|
|
$this->_container = new $this->_activeClass(); |
109
|
|
|
|
110
|
|
|
$this->_isNew = true; |
111
|
|
|
$this->_isDeleted = false; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.