Passed
Branch trunk (7dc288)
by SuperNova.WS
06:07
created

EntityDb::dbUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 \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 \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
  /**
37
   * @return ActiveRecord
38
   */
39
  public function _getContainer() {
40
    return $this->_container;
41
  }
42
43
  /**
44
   * EntityDb constructor.
45
   *
46
   * @param int $id
47
   */
48
  public function __construct($id = 0) {
49
    $this->dbLoadRecord($id);
50
  }
51
52
  /**
53
   * Set flag "for update"
54
   *
55
   * @param bool $forUpdate - DbQuery::DB_FOR_UPDATE | DbQuery::DB_SHARED
56
   */
57
  public function setForUpdate($forUpdate = DbQuery::DB_FOR_UPDATE) {
58
    $className = $this->_activeClass;
59
    $className::setForUpdate($forUpdate);
60
61
    return $this;
62
  }
63
64
  /**
65
   * @param int|float $id
66
   *
67
   * @return ActiveRecord
68
   */
69
  public function dbLoadRecord($id) {
70
    $className = $this->_activeClass;
71
    $this->_container = $className::findById($id);
72
73
    return $this->_container;
74
  }
75
76
  /**
77
   *
78
   */
79
  public function dbUpdate() {
80
    $this->_getContainer()->update();
81
  }
82
83
}
84