Completed
Push — master ( c2185a...82d130 )
by Vitaly
02:29
created

Manager::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 30.11.15
6
 * Time: 16:38
7
 */
8
namespace samsonframework\orm;
9
10
/**
11
 * Database entity manager.
12
 * @package samsonframework\orm
13
 */
14
class Manager
15
{
16
    /** @var string Entity identifier */
17
    protected $entityName;
18
19
    /** @var string Entity primary field name */
20
    protected $primaryFieldName;
21
22
    /** @var array Collection of entity fields that could be used in queries */
23
    protected $queryFields = array();
24
25
    /** @var array Collection of entity field names and their types */
26
    protected $fieldsAndTypes = array();
27
28
    /** @var Database Database manager */
29
    protected $database;
30
31
    /**
32
     * Manager constructor.
33
     *
34
     * @param Database $database database low-level driver
35
     * @param string $entityName Entity name
36
     * @param array $attributes Key-value collection with field name => type
37
     */
38
    public function __construct($database, $entityName, $attributes)
39
    {
40
        $this->database = $database;
41
        $this->entityName = $entityName;
42
        $this->fieldsAndTypes = $attributes;
43
    }
44
45
    /**
46
     * Get new entity instance.
47
     *
48
     * @return RecordInterface New database manager entity instance
49
     */
50
    public function instance()
51
    {
52
        return new $this->entityName($this);
53
    }
54
55
    /**
56
     * Convert RecordInterface instance to collection of its field name => value,
57
     * returning only fields that needs to participate in SQL statements.
58
     * TODO: We need to generate this collection in entity class generation.
59
     *
60
     * @param RecordInterface $object Database record instance to convert
61
     * @return array Collection of key => value with SQL fields statements
62
     */
63 View Code Duplication
    protected function &getQueryFields(RecordInterface &$object = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $collection = array();
66
        foreach ($this->fieldsAndTypes as $attribute => $type) {
67
            if ($type == 'timestamp') {
68
                continue;
69
            } elseif ($this->primaryFieldName == $attribute) {
70
                continue;
71
            }
72
73
            $collection[$attribute] = $object->$attribute;
74
        }
75
76
        return $collection;
77
    }
78
79
//    /**
80
//     * Create new database entity record.
81
//     * @param RecordInterface $entity Entity record for creation
82
//     * @return RecordInterface Created database entity record with new primary identifier
83
//     */
84
//    public function create(RecordInterface $entity)
85
//    {
86
//        $fields = $this->getFields($entity);
87
//
88
//        $this->execute('INSERT INTO `' . $this->entityName . '` (`'
89
//            . implode('`,`', array_keys($fields)) . '`) VALUES (' . implode(',', $fields) . ')'
90
//        );
91
//    }
92
//
93
//    /**
94
//     * Read database entity records from QueryInterface.
95
//     *
96
//     * @param QueryInterface $query For retrieving records
97
//     * @return RecordInterface[] Collection of read database entity records
98
//     */
99
//    public function read(QueryInterface $query)
100
//    {
101
//        // TODO: Implement read() method.
102
//    }
103
//
104
//    /**
105
//     * Update database entity record.
106
//     *
107
//     * @param RecordInterface $entity Entity record for updating
108
//     */
109
//    public function update(RecordInterface $entity)
110
//    {
111
//        // Generate entity fields update command
112
//        $fields = array();
113
//        foreach ($this->getFields($entity) as $fieldName => $fieldValue) {
114
//            $fields[] = '`'.$this->entityName.'`.`'.$fieldName.'` = "'.$fieldValue.'"';
115
//        }
116
//
117
//        $this->execute('UPDATE `' . $this->entityName . '` SET '
118
//            . implode(',', $fields)
119
//            . ' WHERE `' . $this->entityName . '`.`' . $this->primaryFieldName . '`="'
120
//            . $this->quote($entity->id) . '"');
121
//    }
122
//
123
//    /**
124
//     * Delete database record from database.
125
//     *
126
//     * @param RecordInterface $entity Entity record for removing
127
//     */
128
//    public function delete(RecordInterface $entity)
129
//    {
130
//        $this->execute('DELETE FROM `' . $this->entityName . '` WHERE '
131
//            . $this->primaryFieldName . ' = "' . $this->quote($entity->id) . '"'
132
//        );
133
//    }
134
}
135