1 | <?php |
||
11 | final class SQL implements Base |
||
12 | { |
||
13 | protected $doctrine = null; |
||
14 | protected $dbName = null; |
||
15 | |||
16 | public function __construct($config, Connection $client) |
||
17 | { |
||
18 | $this->doctrine = $client; |
||
19 | $this->dbName = $config['db_name']; |
||
20 | } |
||
21 | |||
22 | public function getConnection() |
||
23 | { |
||
24 | return $this->doctrine; |
||
25 | } |
||
26 | |||
27 | public function create($collection, $fields) |
||
28 | { |
||
29 | $schemaManager = $this->doctrine->getSchemaManager(); |
||
30 | $table = new SQLTable($schemaManager, $collection, $fields); |
||
31 | return $table->createTable(); |
||
32 | } |
||
33 | |||
34 | public function drop($collection) |
||
35 | { |
||
36 | $schemaManager = $this->doctrine->getSchemaManager(); |
||
37 | if ($schemaManager->tablesExist([$collection])) { |
||
38 | return $schemaManager->dropTable($collection); |
||
39 | } |
||
40 | return null; |
||
41 | } |
||
42 | |||
43 | public function truncate($collection) |
||
47 | |||
48 | public function createIndexes($collection, $fields) |
||
54 | |||
55 | public function insert($collection, $values) |
||
56 | { |
||
57 | $insertion = $this->doctrine->insert($collection, $values); |
||
58 | if ($insertion !== 0) { |
||
59 | return $this->doctrine->lastInsertId(); |
||
60 | } |
||
61 | return null; |
||
62 | } |
||
63 | |||
64 | public function update($collection, $filter, $values) |
||
68 | |||
69 | public function delete($collection, $filter) |
||
70 | { |
||
71 | $numberOfDeletedItems = $this->doctrine->delete($collection, $filter); |
||
72 | return ($numberOfDeletedItems > 0) ? 1 : 0; |
||
73 | } |
||
74 | |||
75 | public function get($collection, $docId) |
||
79 | |||
80 | public function find($collection, $filters, $fields = null, $sort = null, $offset = 0, $limit = 25) |
||
96 | |||
97 | public function query($collection) |
||
101 | } |
||
102 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: