Completed
Pull Request — master (#10)
by Rougin
02:32
created

CodeigniterModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
/**
6
 * Codeigniter Model
7
 *
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 *
11
 * @property \CI_DB_query_builder $db
12
 */
13
class CodeigniterModel extends \CI_Model
14
{
15
    use Traits\ModelTrait, Traits\RelationshipTrait;
16
17
    /**
18
     * @var \Rougin\Wildfire\Wildfire
19
     */
20
    protected $wildfire;
21
22 36
    public function __construct()
23
    {
24 36
        parent::__construct();
25
26 36
        $this->wildfire = new Wildfire($this->db);
27 36
    }
28
29
    /**
30
     * Returns all of the models from the database.
31
     *
32
     * @return array
33
     */
34 9
    public function all()
35
    {
36 9
        return $this->find_by([]);
37
    }
38
39
    /**
40
     * A wrapper to $this->db->count_all().
41
     *
42
     * @return integer
43
     */
44 3
    public function countAll()
45
    {
46 3
        return $this->db->count_all($this->getTableName());
47
    }
48
49
    /**
50
     * Deletes the specified ID of the model from the database.
51
     *
52
     * @param  integer $id
53
     * @return void
54
     */
55 3
    public function delete($id)
56
    {
57 3
        $this->db->where($this->getPrimaryKey(), $id);
58
59 3
        return $this->db->delete($this->getTableName());
60
    }
61
62
    /**
63
     * Finds the specified model from the database.
64
     *
65
     * @param  integer $id
66
     * @return mixed
67
     */
68 12
    public function find($id)
69
    {
70 12
        return $this->wildfire->find($this->getTableName(), $id);
71
    }
72
73
    /**
74
     * Finds the specified model from the database by the given delimiters.
75
     *
76
     * @param  array $delimiters
77
     * @return mixed
78
     */
79 9
    public function find_by(array $delimiters)
80
    {
81 9
        $this->db->where($delimiters);
82
83 9
        return $this->get()->result();
84
    }
85
86
    /**
87
     * Returns all rows from the specified table.
88
     *
89
     * @return self
90
     */
91 9
    public function get()
92
    {
93 9
        return $this->wildfire->get($this);
94
    }
95
96
    /**
97
     * Inserts a new row into the table.
98
     *
99
     * @param  array $data
100
     * @return integer
101
     */
102 3
    public function insert(array $data)
103
    {
104 3
        $this->db->insert($this->getTableName(), $data);
105
106 3
        return $this->db->insert_id();
0 ignored issues
show
Bug introduced by
The method insert_id() does not exist on CI_DB_query_builder. Did you maybe mean insert()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
107
    }
108
109
    /**
110
     * A wrapper to $this->db->limit().
111
     *
112
     * @param  integer $value
113
     * @param  string  $offset
114
     * @return self
115
     */
116 3
    public function limit($value, $offset = '')
117
    {
118 3
        $this->db->limit($value, $offset);
119
120 3
        return $this;
121
    }
122
123
    /**
124
     * Updates the selected row from the table.
125
     *
126
     * @param  integer $id
127
     * @param  array   $data
128
     * @return boolean
129
     */
130 3
    public function update($id, array $data)
131
    {
132 3
        $this->db->where($this->getPrimaryKey(), $id);
133 3
        $this->db->set($data);
134
135 3
        return $this->db->update($this->getTableName());
136
    }
137
}
138