Bouncy Melons 0.2
declarative style orm
Single.php
Go to the documentation of this file.
1 <?php
2 
3 namespace bm;
4 
5 class EFieldsNotDeclared extends Exception {}
7 class EFieldNotFound extends Exception {}
9 
10 abstract class Single implements ISingle {
11 
12  const TITLE = 'title';
13 
14  protected $data;
15 
19  protected $bm;
20 
21  abstract public function declareFields();
22 
26  public function getFields() {
27 // return $this->declareFields();
28  $bm = $this->getBm();
29  $cache = $bm->getCache();
30  if($cache->cached($this->getSlug(), 'fields')) {
31  return $cache->get($this->getSlug(), 'fields');
32  }
33  $fields = $this->declareFields();
34  if(empty($fields)) {
35  throw new EFieldsNotDeclared("implement \bm\Single::declareFields()");
36  }
37  $cache->set($this->getSlug(),'fields',$fields);
38  return $fields;
39  }
40 
44  public function getTitleField() {
45  $fields = $this->getFields();
46  foreach($fields as $field) {
47  if($field->getName() == self::TITLE) {
48  return $field;
49  }
50  }
51  return reset($fields);
52  }
53 
58  public function getFieldByName($name) {
59  foreach($this->getFields() as $field) {
60  if($field->getName() == $name) {
61  return $field;
62  }
63  }
64  throw new EFieldNotFound("field '".$name."' not found in ".get_class($this));
65  }
66 
67  public function setData($data) {
68  foreach($data as $key=>$value) {
69  $this->set($key, $value);
70  }
71  }
72 
73  public function getData() {
74  return $this->data;
75  }
76 
77  public function getDataForSave() {
78  $re = [];
79  foreach($this->getFields() as $field) {
80  if(empty($this->data[$field->getName()])) {
81  continue;
82  }
83  $re[$field->getName()] = $this->data[$field->getName()];
84  }
85  if(!empty($this->data['id'])) {
86  $re['id'] = $this->data['id'];
87  }
88  return $re;
89  }
90 
91  public function save() {
92  $this->createTableIfNeeded();
93  $bm = $this->getBm();
94  $this->alterTable();
95 // var_dump($this->getData());exit;
96  $data = $this->getData();
97  foreach($this->getFields() as $field) {
98 // var_dump($field);
99  if($field->isRequired()) {
100 // var_dump('here we are');
101 // var_dump($data[$field->getName()]);
102 
103  if(!isset($data[$field->getName()])) {
104 // var_dump('throw?');
105  throw new ERequiredFieldNotSet();
106  }
107  if($field instanceof IdField) {
108  if((int)$data[$field->getName()] < 1) {
109  throw new ERequiredFieldNotSet();
110  }
111  }
112 // exit;
113  }
114  }
115 // exit;
116  $id = $bm->insertOrUpdate($this->getTableName(),$this->getDataForSave());
117  $this->setId($id);
118  return $id;
119  }
120 
121  public function remove() {
122  $bm = $this->getBm();
123  $bm->remove($this->getTableName(),$this->getId());
124  }
125 
126  public function get($fieldName) {
127  $field = $this->getFieldByName($fieldName);
128  return $field->beforeGet($this->getRaw($fieldName));
129  }
130 
134  public function getAll() {
135  $re = [];
136  foreach($this->getFields() as $field) {
137  $re[$field->getName()] = $field->beforeGet($this->getRaw($field->getName()), $this);
138  if($field instanceof IdField
139  && !empty($this->getRaw($field->getName()."_id"))
140  ) {
141  $re[$field->getName()."_id"] = $this->getRaw($field->getName()."_id");
142  }
143  }
144  $re['id'] = $this->getId();
145  return $re;
146  }
147 
148 
149  public function getRaw($fieldName) {
150  return @$this->data[$fieldName];
151  }
152 
153  public function set($fieldName, $value) {
154  try{
155  $field = $this->getFieldByName($fieldName);
156  $this->data[$fieldName] = $field->beforeSet($value);
157  return;
158  } catch(EFieldNotFound $e) {}
159  $this->data[$fieldName] = $value;
160  }
161 
162 
163  public function getId() {
164  return $this->getRaw('id');
165  }
166 
167  public function setId($id) {
168  $this->set('id', $id);
169  }
170 
171  public function createTableIfNeeded() {
172  $bm = $this->getBm();
173  if(!$bm->tableExist($this->getTableName())) {
174  $this->createTable();
175  }
176  }
177 
178  public function getTableName() {
179  return str_replace("\\",".",strtolower(get_class($this)));
180  }
181 
182  public function getSlug() {
183  return str_replace("\\",".",strtolower(get_class($this)));
184  }
185 
186  public function createTable() {
187  $sqlFields = [];
188  foreach($this->getFields() as $field) {
189  if($field->getOption('calc')) {
190  continue;
191  }
192  $sqlFields[] ="`".$field->getName()."` ".$field->getSqlType();
193  }
194  $bm = $this->getBm();
195 
196  $sql = "CREATE TABLE `".$this->getTableName()."`(
197  ".$bm->getCreatePrimaryId().",
198  ".implode(",",$sqlFields)."
199  ) ".$bm->getCreateDefaultCharset();
200  $bm->exec($sql);
201  }
202 
203  public function alterTable() {
204  $bm = $this->getBm();
205  $dbFields = $bm->getTableFields($this->getTableName());
206  $dbFieldNames = array_column($dbFields, 'field');
207  foreach($this->getFields() as $field) {
208  if(!in_array($field->getName(), $dbFieldNames)) {
209  $this->addField($field);
210  continue;
211  }
212  foreach($dbFields as $dbField) {
213  if($field->getName()==$dbField['field']){
214  if($field->getSqlType() != $dbField['type']) {
215  $this->updateFieldType($field);
216  }
217  continue;
218  }
219  }
220  }
221  }
222 
223  public function addField($field) {
224  $sql = "
225  ALTER TABLE
226  `".$this->getTableName()."`
227  ADD
228  `".$field->getName()."` ".$field->getSqlType()."
229  ";
230  $bm = $this->getBm();
231  $bm->exec($sql);
232  }
233 
234  public function updateFieldType($field) {
235  $sql = "
236  ALTER TABLE
237  `".$this->getTableName()."`
238  MODIFY COLUMN
239  `".$field->getName()."` ".$field->getSqlType()."
240  ";
241  $bm = $this->getBm();
242  $bm->exec($sql);
243  }
244 
245  public function getBm() {
246  if(empty($this->bm)) {
247  throw new EBouncyMelonsInstanceNotSet("use \bm\Single::setBm()");
248  }
249  return $this->bm;
250  }
251 
252  public function setBm(BouncyMelons $bm) {
253  $this->bm = $bm;
254  }
255 }
setId($id)
Definition: Single.php:167
updateFieldType($field)
Definition: Single.php:234
getDataForSave()
Definition: Single.php:77
getFields()
Definition: Single.php:26
getRaw($fieldName)
Definition: Single.php:149
setBm(BouncyMelons $bm)
Definition: Single.php:252
getAll()
Definition: Single.php:134
save()
Definition: Single.php:91
getTableName()
Definition: Single.php:178
getTitleField()
Definition: Single.php:44
getData()
Definition: Single.php:73
alterTable()
Definition: Single.php:203
createTableIfNeeded()
Definition: Single.php:171
setData($data)
Definition: Single.php:67
getSlug()
Definition: Single.php:182
getFieldByName($name)
Definition: Single.php:58
createTable()
Definition: Single.php:186
addField($field)
Definition: Single.php:223