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->find_by([]); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Deletes the specified ID of the model from the database. |
45
|
|
|
* |
46
|
|
|
* @param integer $id |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
3 |
|
public function delete($id) |
50
|
|
|
{ |
51
|
3 |
|
$item = $this->find($id); |
52
|
|
|
|
53
|
3 |
|
if (! is_null($item)) { |
54
|
3 |
|
$this->credo->remove($item); |
55
|
3 |
|
$this->credo->flush(); |
56
|
3 |
|
} |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Finds an entity by its primary key / identifier. |
61
|
|
|
* |
62
|
|
|
* @param mixed $id |
63
|
|
|
* @param int|null $lockMode |
64
|
|
|
* @param int|null $lockVersion |
65
|
|
|
* @return object|null |
66
|
|
|
*/ |
67
|
9 |
|
public function find($id, $lockMode = null, $lockVersion = null) |
68
|
|
|
{ |
69
|
9 |
|
return $this->repository->find($id, $lockMode, $lockVersion); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Finds models by a set of criteria. |
74
|
|
|
* |
75
|
|
|
* @param array $criteria |
76
|
|
|
* @param array|null $orderBy |
77
|
|
|
* @param integer|null $limit |
78
|
|
|
* @param integer|null $offset |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
3 |
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
82
|
|
|
{ |
83
|
3 |
|
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Inserts a new row into the table. |
88
|
|
|
* |
89
|
|
|
* @param array $data |
90
|
|
|
* @return integer |
91
|
|
|
*/ |
92
|
3 |
|
public function insert(array $data) |
93
|
|
|
{ |
94
|
3 |
|
$factory = $this->credo->getMetadataFactory(); |
95
|
3 |
|
$metadata = $factory->getMetadataFor(get_class($this)); |
96
|
|
|
|
97
|
3 |
|
$this->db->insert($metadata->getTableName(), $data); |
98
|
|
|
|
99
|
3 |
|
$lastId = $this->db->insert_id(); |
|
|
|
|
100
|
|
|
|
101
|
3 |
|
$this->credo->refresh($this->find($lastId)); |
|
|
|
|
102
|
|
|
|
103
|
3 |
|
return $lastId; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Updates the selected row from the table. |
108
|
|
|
* |
109
|
|
|
* @param integer $id |
110
|
|
|
* @param array $data |
111
|
|
|
* @return boolean |
112
|
|
|
*/ |
113
|
3 |
|
public function update($id, array $data) |
114
|
|
|
{ |
115
|
3 |
|
$factory = $this->credo->getMetadataFactory(); |
116
|
3 |
|
$metadata = $factory->getMetadataFor(get_class($this)); |
117
|
|
|
|
118
|
3 |
|
$this->db->where($metadata->getSingleIdentifierColumnName(), $id); |
119
|
3 |
|
$this->db->set($data); |
120
|
|
|
|
121
|
3 |
|
$result = $this->db->update($metadata->getTableName()); |
122
|
|
|
|
123
|
3 |
|
$this->credo->refresh($this->find($id)); |
|
|
|
|
124
|
|
|
|
125
|
3 |
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Calls methods from CodeigniterModel in underscore case. |
130
|
|
|
* |
131
|
|
|
* @param string $method |
132
|
|
|
* @param mixed $parameters |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
3 |
|
public function __call($method, $parameters) |
136
|
|
|
{ |
137
|
3 |
|
return \Rougin\Credo\Helpers\MagicMethodHelper::call($this, $method, $parameters); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: