1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elchroy\PotatoORM; |
4
|
|
|
|
5
|
|
|
class PotatoModel |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* [$queryTo The query (instance of PotatoQuery) to handle all database queries. |
9
|
|
|
* |
10
|
|
|
* @var [type] Instance of potatoQuery class, to be difined during construction. |
11
|
|
|
*/ |
12
|
|
|
public $queryTo; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* [$dataToSave The properties of a object of theis class to be saved in the database. |
16
|
|
|
* |
17
|
|
|
* @var array This is an array of property names to be saved in to the database. |
18
|
|
|
*/ |
19
|
|
|
public $dataToSave = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* [__construct The constructor to initialize public variables]. |
23
|
|
|
* |
24
|
|
|
* @param PotatoQuery|null $potatoQuery The query to be used for the communication with the databse. |
25
|
|
|
*/ |
26
|
|
|
public function __construct(PotatoQuery $potatoQuery = null) |
27
|
|
|
{ |
28
|
|
|
$this->queryTo = $potatoQuery == null ? new PotatoQuery() : $potatoQuery; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* [__set magic Property to set put of the object into the arary of properties to be saved in the database.]. |
33
|
|
|
* |
34
|
|
|
* @param [type] $property [The property of the object to be saved.] |
|
|
|
|
35
|
|
|
* @param [type] $value [The value of given property to be saved.] |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __set($property, $value) |
38
|
|
|
{ |
39
|
|
|
$this->dataToSave[$property] = $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* [__get Magic function to get the property from the dataToSave array, the array of properties.]. |
44
|
|
|
* |
45
|
|
|
* @param [type] $property [The property to be gotten from the data to save array of propoerties.] |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @return [type] [If the property is cound, return the value of that property. Otherwise throw an exception] |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function __get($property) |
50
|
|
|
{ |
51
|
|
|
if (array_key_exists($property, $this->dataToSave)) { |
52
|
|
|
return $this->dataToSave[$property]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return "$property not found."; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* [__isset Magic function to check if the given propertyh of the object is set.]. |
60
|
|
|
* |
61
|
|
|
* @param [type] $property [The property in question] |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return bool True if the property exists. False otherwise. |
64
|
|
|
*/ |
65
|
|
|
public function __isset($property) |
66
|
|
|
{ |
67
|
|
|
return isset($this->dataToSave[$property]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* [getAll Get all records of database table corresponding the the called class name. |
72
|
|
|
* |
73
|
|
|
* @param [type] $query [The query to be used to communicated with the databse.] |
|
|
|
|
74
|
|
|
* |
75
|
|
|
* @return [type] Return the returned value of the query's query. The return value is an array of records represented as objects. |
|
|
|
|
76
|
|
|
*/ |
77
|
|
|
public static function getAll($query = null) |
78
|
|
|
{ |
79
|
|
|
$query = $query == null ? new PotatoQuery() : $query; |
80
|
|
|
$table = self::getClassTableName(); |
81
|
|
|
|
82
|
|
|
return $query->getFrom($table); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* [find Find on record or row in the table given by an ID. |
87
|
|
|
* |
88
|
|
|
* @param [type] $id [the ID of the row record to be fetched.] |
|
|
|
|
89
|
|
|
* @param [type] $query [The query to be used for the database communication.] |
|
|
|
|
90
|
|
|
* |
91
|
|
|
* @return [type] [The returned value of finding the record with the given ID.] |
|
|
|
|
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public static function find($id, $query = null) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$query = $query == null ? new PotatoQuery() : $query; |
96
|
|
|
$table = self::getClassTableName(); |
97
|
|
|
|
98
|
|
|
return $query->getOne($table, $id); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* SAve a recrd into the database. Saveing is either inserting or updating. |
103
|
|
|
* |
104
|
|
|
* @return [type] [A newly inserted record ot a recently updated record in the database.] |
|
|
|
|
105
|
|
|
*/ |
106
|
|
|
public function save() |
107
|
|
|
{ |
108
|
|
|
return $this->isStored() ? $this->update() : $this->insert(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Check if the object to be saved has an ID. |
113
|
|
|
* |
114
|
|
|
* @return bool [description] |
115
|
|
|
*/ |
116
|
|
|
public function isStored() |
117
|
|
|
{ |
118
|
|
|
return isset($this->id); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Insert a new record into the database while communicating with the qury handler. |
123
|
|
|
* |
124
|
|
|
* @return [type] [The returned value of the quering the databse.] |
|
|
|
|
125
|
|
|
*/ |
126
|
|
|
public function insert() |
127
|
|
|
{ |
128
|
|
|
$table = self::getClassTableName(); |
129
|
|
|
|
130
|
|
|
return $this->queryTo->storeIn($table, $this->dataToSave); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Update a record in a database. The record must have an ID before it can be updated. |
135
|
|
|
* |
136
|
|
|
* @return [type] [description] |
|
|
|
|
137
|
|
|
*/ |
138
|
|
|
public function update() |
139
|
|
|
{ |
140
|
|
|
$table = self::getClassTableName(); |
141
|
|
|
|
142
|
|
|
return $this->queryTo->updateAt($table, $this->dataToSave); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Destroy a recored or row from a database table. |
147
|
|
|
* |
148
|
|
|
* @param [type] $id [The ID of the record to be deleted.] |
|
|
|
|
149
|
|
|
* @param [type] $query [The query to handle to deleting operation] |
|
|
|
|
150
|
|
|
* |
151
|
|
|
* @return [type] [An array of all the records in that table without the deleted recode.] |
|
|
|
|
152
|
|
|
*/ |
153
|
|
View Code Duplication |
public static function destroy($id, $query = null) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$query = $query == null ? new PotatoQuery() : $query; |
156
|
|
|
$table = self::getClassTableName(); |
157
|
|
|
$query->deleteFrom($table, $id); |
158
|
|
|
|
159
|
|
|
return self::getAll($query); // return all the items after the previous item has been deleted. |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* [getClassTableName Get the name of the class from which this function is called, including classes that inherit from this class.]. |
164
|
|
|
* |
165
|
|
|
* @return [type] [description] |
|
|
|
|
166
|
|
|
*/ |
167
|
|
|
public static function getClassTableName() |
168
|
|
|
{ |
169
|
|
|
$tableWithNameSpance = strtolower(get_called_class()); |
170
|
|
|
$table = explode('\\', $tableWithNameSpance); |
171
|
|
|
|
172
|
|
|
return end($table); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.