Issues (9)

src/Table/AbstractTable.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Table;
4
5
use Cake\Database\Connection;
6
use Cake\Database\Query;
7
use Cake\Database\StatementInterface;
8
9
/**
10
 * Class AbstractTable.
11
 */
12
abstract class AbstractTable implements ModelInterface
13
{
14
    protected $table = null;
15
16
    protected $connection = null;
17
18
    /**
19
     * AppTable constructor.
20
     *
21
     * @todo rename model to table
22
     *
23
     * @param Connection $connection
24
     */
25
    public function __construct(Connection $connection = null)
26
    {
27
        $this->connection = $connection;
28
    }
29
30
    /**
31
     * Get Query.
32
     *
33
     * @return Query
34
     */
35
    protected function newSelect(): Query
36
    {
37
        return $this->connection->newQuery()->from($this->table);
0 ignored issues
show
The method newQuery() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        return $this->connection->/** @scrutinizer ignore-call */ newQuery()->from($this->table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
    }
39
40
    /**
41
     * Get all entries from database.
42
     *
43
     * @return array $rows
44
     */
45
    protected function getAll(): array
46
    {
47
        $query = $this->newSelect();
48
        $query->select('*');
49
        $rows = $query->execute()->fetchAll('assoc');
50
51
        return $rows;
52
    }
53
54
    /**
55
     * Insert into database.
56
     *
57
     * @param array $row with data to insertUser into database
58
     *
59
     * @return StatementInterface
60
     */
61
    protected function insert(array $row): StatementInterface
62
    {
63
        return $this->connection->insert($this->table, $row);
64
    }
65
66
    /**
67
     * Update database.
68
     *
69
     * @param string $where should be the id
70
     * @param array $row
71
     *
72
     * @return StatementInterface
73
     */
74
    protected function update(array $row, string $where): StatementInterface
75
    {
76
        $query = $this->connection->newQuery();
77
        $query->update($this->table)
78
            ->set($row)
79
            ->where(['id' => $where]);
80
81
        return $query->execute();
82
    }
83
84
    /**
85
     * Delete from database.
86
     *
87
     * @param string $id
88
     *
89
     * @return StatementInterface
90
     */
91
    protected function delete(string $id): StatementInterface
92
    {
93
        return $this->connection->delete($this->table, ['id' => $id]);
94
    }
95
}
96