1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Credo; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Codeigniter Model |
7
|
|
|
* |
8
|
|
|
* @package Credo |
9
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @property \CI_DB_query_builder $db |
12
|
|
|
*/ |
13
|
|
|
class CodeigniterModel extends \CI_Model |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Rougin\Credo\Credo |
17
|
|
|
*/ |
18
|
|
|
protected $credo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Doctrine\ORM\EntityRepository |
22
|
|
|
*/ |
23
|
|
|
protected $repository; |
24
|
|
|
|
25
|
3 |
|
public function __construct() |
26
|
|
|
{ |
27
|
3 |
|
parent::__construct(); |
28
|
|
|
|
29
|
3 |
|
$this->credo = new Credo($this->db); |
30
|
3 |
|
$this->repository = $this->credo->getRepository(get_class($this)); |
31
|
3 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns all of the models from the database. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
3 |
|
public function all() |
39
|
|
|
{ |
40
|
3 |
|
return $this->findBy([]); |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a total rows from the specified table. |
45
|
|
|
* |
46
|
|
|
* @return integer |
47
|
|
|
*/ |
48
|
3 |
|
public function countAll() |
49
|
|
|
{ |
50
|
3 |
|
list($tableName, $primaryKey) = $this->getTableNameAndPrimaryKey(); |
51
|
|
|
|
52
|
3 |
|
$queryBuilder = $this->credo->createQueryBuilder(); |
53
|
|
|
|
54
|
3 |
|
$queryBuilder->select($queryBuilder->expr()->count($tableName . '.' . $primaryKey)); |
55
|
3 |
|
$queryBuilder->from(get_class($this), $tableName); |
56
|
|
|
|
57
|
3 |
|
return $queryBuilder->getQuery()->getSingleScalarResult(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Deletes the specified ID of the model from the database. |
62
|
|
|
* |
63
|
|
|
* @param integer $id |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
3 |
|
public function delete($id) |
67
|
|
|
{ |
68
|
3 |
|
$item = $this->find($id); |
69
|
|
|
|
70
|
3 |
|
if (! is_null($item)) { |
71
|
3 |
|
$this->credo->remove($item); |
72
|
3 |
|
$this->credo->flush(); |
73
|
3 |
|
} |
74
|
3 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Finds an entity by its primary key / identifier. |
78
|
|
|
* |
79
|
|
|
* @param mixed $id |
80
|
|
|
* @param int|null $lockMode |
81
|
|
|
* @param int|null $lockVersion |
82
|
|
|
* @return object|null |
83
|
|
|
*/ |
84
|
9 |
|
public function find($id, $lockMode = null, $lockVersion = null) |
85
|
|
|
{ |
86
|
9 |
|
return $this->repository->find($id, $lockMode, $lockVersion); |
87
|
3 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Finds models by a set of criteria. |
91
|
|
|
* |
92
|
|
|
* @param array $criteria |
93
|
|
|
* @param array|null $orderBy |
94
|
|
|
* @param integer|null $limit |
95
|
|
|
* @param integer|null $offset |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
6 |
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
99
|
|
|
{ |
100
|
6 |
|
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Inserts a new row into the table. |
105
|
|
|
* |
106
|
|
|
* @param array $data |
107
|
|
|
* @return integer |
108
|
|
|
*/ |
109
|
3 |
|
public function insert(array $data) |
110
|
|
|
{ |
111
|
3 |
|
list($tableName) = $this->getTableNameAndPrimaryKey(); |
112
|
|
|
|
113
|
3 |
|
$this->db->insert($tableName, $data); |
114
|
|
|
|
115
|
3 |
|
$lastId = $this->db->insert_id(); |
|
|
|
|
116
|
|
|
|
117
|
3 |
|
$this->credo->refresh($this->find($lastId)); |
|
|
|
|
118
|
|
|
|
119
|
3 |
|
return $lastId; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Updates the selected row from the table. |
124
|
|
|
* |
125
|
|
|
* @param integer $id |
126
|
|
|
* @param array $data |
127
|
|
|
* @return boolean |
128
|
|
|
*/ |
129
|
3 |
|
public function update($id, array $data) |
130
|
|
|
{ |
131
|
3 |
|
list($tableName, $primaryKey) = $this->getTableNameAndPrimaryKey(); |
132
|
|
|
|
133
|
3 |
|
$this->db->where($primaryKey, $id); |
134
|
3 |
|
$this->db->set($data); |
135
|
|
|
|
136
|
3 |
|
$result = $this->db->update($tableName); |
137
|
|
|
|
138
|
3 |
|
$this->credo->refresh($this->find($id)); |
|
|
|
|
139
|
|
|
|
140
|
3 |
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the table name and the corresponding primary key. |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
9 |
|
protected function getTableNameAndPrimaryKey() |
149
|
|
|
{ |
150
|
9 |
|
$factory = $this->credo->getMetadataFactory(); |
151
|
9 |
|
$metadata = $factory->getMetadataFor(get_class($this)); |
152
|
|
|
|
153
|
9 |
|
return [ $metadata->getTableName(), $metadata->getSingleIdentifierColumnName() ]; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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.