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 $db |
12
|
|
|
* @method \Doctrine\ORM\EntityRepository getRepository(string $entityName) |
13
|
|
|
* @method void remove(object $entity) |
14
|
|
|
* @method void flush(null|object|array $entity) |
15
|
|
|
*/ |
16
|
|
|
class CodeigniterModel extends \CI_Model |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Rougin\Credo\Credo |
20
|
|
|
*/ |
21
|
|
|
protected $credo; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Doctrine\ORM\EntityRepository |
25
|
|
|
*/ |
26
|
|
|
protected $repository; |
27
|
|
|
|
28
|
3 |
|
public function __construct() |
29
|
|
|
{ |
30
|
3 |
|
parent::__construct(); |
31
|
|
|
|
32
|
3 |
|
$this->credo = new Credo($this->db); |
|
|
|
|
33
|
3 |
|
$this->repository = $this->credo->getRepository(get_class($this)); |
|
|
|
|
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns all of the models from the database. |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
3 |
|
public function all() |
42
|
|
|
{ |
43
|
3 |
|
return $this->find_by([]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Deletes the specified ID of the model from the database. |
48
|
|
|
* |
49
|
|
|
* @param integer $id |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
3 |
|
public function delete($id) |
53
|
|
|
{ |
54
|
3 |
|
$item = $this->find($id); |
55
|
|
|
|
56
|
3 |
|
$this->credo->remove($item); |
|
|
|
|
57
|
3 |
|
$this->credo->flush(); |
|
|
|
|
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Finds an entity by its primary key / identifier. |
62
|
|
|
* |
63
|
|
|
* @param mixed $id |
64
|
|
|
* @param int|null $lockMode |
65
|
|
|
* @param int|null $lockVersion |
66
|
|
|
* @return object|null |
67
|
|
|
*/ |
68
|
9 |
|
public function find($id, $lockMode = null, $lockVersion = null) |
69
|
|
|
{ |
70
|
9 |
|
return $this->repository->find($id, $lockMode, $lockVersion); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Finds models by a set of criteria. |
75
|
|
|
* |
76
|
|
|
* @param array $criteria |
77
|
|
|
* @param array|null $orderBy |
78
|
|
|
* @param integer|null $limit |
79
|
|
|
* @param integer|null $offset |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
3 |
|
public function find_by(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
83
|
|
|
{ |
84
|
3 |
|
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Inserts a new row into the table. |
89
|
|
|
* |
90
|
|
|
* @param array $data |
91
|
|
|
* @return integer |
92
|
|
|
*/ |
93
|
3 |
|
public function insert(array $data) |
94
|
|
|
{ |
95
|
3 |
|
$factory = $this->credo->getMetadataFactory(); |
|
|
|
|
96
|
3 |
|
$metadata = $factory->getMetadataFor(get_class($this)); |
97
|
|
|
|
98
|
3 |
|
$this->db->insert($metadata->getTableName(), $data); |
99
|
|
|
|
100
|
3 |
|
$lastId = $this->db->insert_id(); |
|
|
|
|
101
|
|
|
|
102
|
3 |
|
$this->credo->refresh($this->find($lastId)); |
|
|
|
|
103
|
|
|
|
104
|
3 |
|
return $lastId; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Updates the selected row from the table. |
109
|
|
|
* |
110
|
|
|
* @param integer $id |
111
|
|
|
* @param array $data |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
3 |
|
public function update($id, array $data) |
115
|
|
|
{ |
116
|
3 |
|
$factory = $this->credo->getMetadataFactory(); |
|
|
|
|
117
|
3 |
|
$metadata = $factory->getMetadataFor(get_class($this)); |
118
|
|
|
|
119
|
3 |
|
$this->db->where($metadata->getSingleIdentifierColumnName(), $id); |
120
|
3 |
|
$this->db->set($data); |
121
|
|
|
|
122
|
3 |
|
$result = $this->db->update($metadata->getTableName()); |
123
|
|
|
|
124
|
3 |
|
$this->credo->refresh($this->find($id)); |
|
|
|
|
125
|
|
|
|
126
|
3 |
|
return $result; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: