Completed
Push — work-fleets ( addb18...c7cb6b )
by SuperNova.WS
06:04
created

DbRowDirectOperator::getById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
ccs 0
cts 8
cp 0
crap 2
1
<?php
2
3
/**
4
 * Class DbRowDirectOperator
5
 *
6
 * Handle Entity storing/loading operations
7
 */
8
9
class DbRowDirectOperator implements \Common\IEntityOperator {
10
11
  /**
12
   * @param Entity $entity
13
   */
14
  public function getById($entity) {
15
    $stmt = classSupernova::$gc->query
0 ignored issues
show
Bug introduced by
The method setIdField cannot be called on \classSupernova::$gc->query (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
16
      ->setIdField($entity->getIdFieldName())
17
      ->field('*')
18
      ->from($entity->getTableName())
19
      ->where($entity->getIdFieldName() . ' = "' . $entity->dbId . '"');
20
21
    $entity->importDbRow($stmt->selectRow());
22
  }
23
24
  /**
25
   * @param Entity $entity
26
   */
27
  public function deleteById($entity) {
28
    $db = $entity->getDbStatic();
29
30
    $db->doquery("DELETE FROM `{{" . $entity->getTableName() . "}}` WHERE `{$entity->getIdFieldName()}` = '{$entity->dbId}' LIMIT 1;");
31
32
    return $db->db_affected_rows();
33
  }
34
35
  /**
36
   * @param Entity $entity
37
   */
38
  public function insert($entity) {
39
    $db = $entity->getDbStatic();
40
41
    $query = array();
42
    foreach ($entity->exportRowWithoutId() as $fieldName => $fieldValue) {
43
      // TODO: MORE type detection
44
      if(!is_numeric($fieldValue)) {
45
        $fieldValue = "'" . $db->db_escape($fieldValue) . "'";
46
      }
47
      $query[] = "`{$fieldName}` = {$fieldValue}";
48
    }
49
50
    $query = implode(',', $query);
51
    if (empty($query)) {
52
      // TODO Exceptiion
53
      return 0;
54
    }
55
56
    $db->doquery("INSERT INTO `{{" . $entity->getTableName() . "}}` SET " . $query);
57
58
    // TODO Exceptiion if db_insert_id() is empty
59
    return $entity->dbId = $db->db_insert_id();
60
  }
61
62
}
63