Bouncy Melons 0.1
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 {}
8 
9 abstract class Single implements ISingle {
10 
11  const TITLE = 'title';
12 
13  protected $data;
14 
18  protected $bm;
19 
20  abstract public function declareFields();
21 
25  public function getFields() {
26 // return $this->declareFields();
27  $bm = $this->getBm();
28  $cache = $bm->getCache();
29  if($cache->cached($this->getSlug(), 'fields')) {
30  return $cache->get($this->getSlug(), 'fields');
31  }
32  $fields = $this->declareFields();
33  if(empty($fields)) {
34  throw new EFieldsNotDeclared("implement \bm\Single::declareFields()");
35  }
36  $cache->set($this->getSlug(),'fields',$fields);
37  return $fields;
38  }
39 
40  public function getTitleField() {
41  $fields = $this->getFields();
42  foreach($fields as $field) {
43  if($field->getName() == self::TITLE) {
44  return $field;
45  }
46  }
47  return reset($fields);
48  }
49 
54  public function getFieldByName($name) {
55  foreach($this->getFields() as $field) {
56  if($field->getName() == $name) {
57  return $field;
58  }
59  }
60  throw new EFieldNotFound("field '".$name."' not found in ".get_class($this));
61  }
62 
63  public function setData($data) {
64  foreach($data as $key=>$value) {
65  $this->set($key, $value);
66  }
67  }
68 
69  public function getData() {
70  return $this->data;
71  }
72 
73  public function save() {
74  $this->createTableIfNeeded();
75  $bm = $this->getBm();
76  $this->alterTable();
77 // var_dump($this->getData());exit;
78 // $data = [];
79 // foreach($this->getFields() as $field) {
80 // $data[$field->getName()] = $data
81 // }
82  $id = $bm->insertOrUpdate($this->getTableName(),$this->getData());
83  $this->setId($id);
84  return $id;
85  }
86 
87  public function remove() {
88  $bm = $this->getBm();
89  $bm->remove($this->getTableName(),$this->getId());
90  }
91 
92  public function get($fieldName) {
93  $field = $this->getFieldByName($fieldName);
94  return $field->beforeGet($this->getRaw($fieldName), $this);
95  }
96 
100  public function getAll() {
101  $re = [];
102  foreach($this->getFields() as $field) {
103  $re[$field->getName()] = $field->beforeGet($this->getRaw($field->getName()), $this);
104  if($field instanceof IdField
105  && !empty($this->getRaw($field->getName()."_id"))
106  ) {
107  $re[$field->getName()."_id"] = $this->getRaw($field->getName()."_id");
108  }
109  }
110  $re['id'] = $this->getId();
111  return $re;
112  }
113 
114 
115  public function getRaw($fieldName) {
116  return @$this->data[$fieldName];
117  }
118 
119  public function set($fieldName, $value) {
120  try{
121  $field = $this->getFieldByName($fieldName);
122  $this->data[$fieldName] = $field->beforeSet($value, $this);
123  return;
124  } catch(EFieldNotFound $e) {}
125  $this->data[$fieldName] = $value;
126  }
127 
128 
129  public function getId() {
130  return $this->getRaw('id');
131  }
132 
133  public function setId($id) {
134  $this->set('id', $id);
135  }
136 
137  public function createTableIfNeeded() {
138  $bm = $this->getBm();
139  if(!$bm->tableExist($this->getTableName())) {
140  $this->createTable();
141  }
142  }
143 
144  public function getTableName() {
145  return str_replace("\\",".",strtolower(get_class($this)));
146  }
147 
148  public function getSlug() {
149  return str_replace("\\",".",strtolower(get_class($this)));
150  }
151 
152  public function createTable() {
153  $sqlFields = [];
154  foreach($this->getFields() as $field) {
155  $sqlFields[] ="`".$field->getName()."` ".$field->getSqlType();
156  }
157  $bm = $this->getBm();
158 
159  $sql = "CREATE TABLE `".$this->getTableName()."`(
160  ".$bm->getCreatePrimaryId().",
161  ".implode(",",$sqlFields)."
162  ) ".$bm->getCreateDefaultCharset();
163  $bm->exec($sql);
164  }
165 
166  public function alterTable() {
167  $bm = $this->getBm();
168  $dbFields = $bm->getTableFields($this->getTableName());
169  $dbFieldNames = array_column($dbFields, 'field');
170  foreach($this->getFields() as $field) {
171  if(!in_array($field->getName(), $dbFieldNames)) {
172  $this->addField($field);
173  continue;
174  }
175  foreach($dbFields as $dbField) {
176  if($field->getName()==$dbField['field']){
177  if($field->getSqlType() != $dbField['type']) {
178  $this->updateFieldType($field);
179  }
180  continue;
181  }
182  }
183  }
184  }
185 
186  public function addField($field) {
187  $sql = "
188  ALTER TABLE
189  `".$this->getTableName()."`
190  ADD
191  `".$field->getName()."` ".$field->getSqlType()."
192  ";
193  $bm = $this->getBm();
194  $bm->exec($sql);
195  }
196 
197  public function updateFieldType($field) {
198  $sql = "
199  ALTER TABLE
200  `".$this->getTableName()."`
201  MODIFY COLUMN
202  `".$field->getName()."` ".$field->getSqlType()."
203  ";
204  $bm = $this->getBm();
205  $bm->exec($sql);
206  }
207 
208  public function getBm() {
209  if(empty($this->bm)) {
210  throw new EBouncyMelonsInstanceNotSet("use \bm\Single::setBm()");
211  }
212  return $this->bm;
213  }
214 
215  public function setBm(BouncyMelons $bm) {
216  $this->bm = $bm;
217  }
218 }
setId($id)
Definition: Single.php:133
updateFieldType($field)
Definition: Single.php:197
getFields()
Definition: Single.php:25
getRaw($fieldName)
Definition: Single.php:115
setBm(BouncyMelons $bm)
Definition: Single.php:215
getAll()
Definition: Single.php:100
save()
Definition: Single.php:73
getTableName()
Definition: Single.php:144
getTitleField()
Definition: Single.php:40
getData()
Definition: Single.php:69
alterTable()
Definition: Single.php:166
createTableIfNeeded()
Definition: Single.php:137
setData($data)
Definition: Single.php:63
getSlug()
Definition: Single.php:148
getFieldByName($name)
Definition: Single.php:54
createTable()
Definition: Single.php:152
addField($field)
Definition: Single.php:186