Bouncy Melons 0.2
declarative style orm
DataList.php
Go to the documentation of this file.
1 <?php
2 
3 namespace bm;
4 
5 class DataList {
6 
7  protected $vmodel;
8 
9  public function __construct(IVModel $vmodel) {
10  $this->vmodel = $vmodel;
11  }
12 
13  public function getTableName() {
14  $single = $this->createSingle();
15  return $single->getTableName();
16  }
17 
18  public function getFields() {
19  $single = $this->createSingle();
20  return $single->getFields();
21  }
22 
23  public function getViewName() {
24  return $this->getTableName()."_view";
25  }
26 
31  public function find(IFilter $filter) {
32  $this->createViewIfNeeded();
33  $bm = $this->getBm();
34  $filter->setBm($bm);
35  $fields = $this->getFields();
36  $where = $filter->createWhere($fields);
37  $orderby = $filter->createOrderBy($fields);
38  $limit = $filter->createLimit();
39 
40  $data = $bm->select($this->getViewName()
41  , $this->getFieldNames()
42  , $where, $limit, $orderby
43  , $filter->createGroupBy()
44  );
45 
46  $re = [];
47  foreach($data as $itemData) {
48  $item = $this->createSingle();
49  $item->setData($itemData);
50  $re[] = $item;
51  }
52  return $re;
53  }
54 
55  public function getFieldNames() {
56  $re = ['id'];
57  foreach($this->getFields() as $field) {
58  $re[] = $field->getName();
59  if($field instanceof IdField) {
60  $re[] = $field->getName()."_id";
61  }
62  }
63  return $re;
64  }
65 
66 
72  public function findOne(IFilter $filter) {
73  $items = $this->find($filter);
74  if(empty($items)) {
75  throw new EItemNotFound();
76  }
77  return reset($items);
78  }
79 
80 
85  public function getTotal(IFilter $filter) {
86  $this->createViewIfNeeded();
87  $fields = $this->getFields();
88  $bm = $this->getBm();
89  $wr = '';
90  $where = $filter->createWhere($fields);
91  if(!empty($where)) {
92  $wr = "WHERE ".$where;
93  }
94  return (int)$bm->queryParam("SELECT COUNT(*) FROM `".$this->getViewName()."` ".$wr);
95  }
96 
101  public function getMaxPages(IFilter $filter) {
102  $total = $this->getTotal($filter);
103  $itemsPerPage = $filter->getItemsPerPage();
104  return (int)ceil($total/$itemsPerPage);
105  }
106 
113  public function fullfind(IFilter $filter) {
114  return [
115  $this->find($filter),// items
116  $this->getTotal($filter),//total
117  $this->getMaxPages($filter),//pages
118  ];
119  }
120 
124  public function truncate() {
125  $this->getBm()->truncate($this->getTableName());
126  }
127 
128  public function getOne($id, $column='id') {
129  $item = $this->createSingle();
130  $bm = $this->getBm();
131  $data = $bm->selectOne($this->getTableName(),"`".$column."`='".$id."'");
132  $item->setData($data);
133  return $item;
134  }
135 
136  public function readOne($id, $column='id') {
137  $this->createViewIfNeeded();
138  $item = $this->createSingle();
139  $bm = $this->getBm();
140  $data = $bm->selectOne($this->getViewName(),"`".$column."`='".$id."'");
141  $item->setData($data);
142  return $item;
143  }
144 
145  public function createViewIfNeeded() {
146  $bm = $this->getBm();
147  $cache = $bm->getCache();
148  if($cache->get($this->getSlug(), 'viewCreated')) {
149  return;
150  }
151  try {
152  $this->declareView();
153  } catch(EExec $e) {
154  // try to fix common issue automatically
155  $this->createTableIfNeeded();
156  $this->declareView();
157  }
158  //$this->createTableIfNeeded();///@todo how we should deal with non existing empty tables on read? what if view does not require table?
159 // $this->declareView();
160  $cache->set($this->getSlug(), 'viewCreated', true);
161  }
162 
163  public function createTableIfNeeded() {
164  $single = $this->createSingle();
165  $single->createTableIfNeeded();
166  $single->alterTable();
167  }
168 
169  public function declareView() {
170  $joins = $this->createJoins();
171  $fieldsToSelect = $this->createFieldsToSelect();
172  $sqlFields = implode(',',$fieldsToSelect);
173  $this->createViewAs("
174  SELECT
175  `".$this->getTableName()."`.id, ".$sqlFields."
176  FROM
177  `".$this->getTableName()."`
178  ".$joins);
179  }
180 
181  public function createViewAs($sql) {
182  $bm = $this->getBm();
183  $bm->createViewAs($this->getViewName(), $sql);
184  }
185 
186  public function createJoins() {
187  $bm = $this->getBm();
188  $joins = '';
189  foreach($this->getFields() as $field) {
190  if($field->getName() == null) {
191  continue;
192  }
193  if($field instanceof IdField) {
194  $viewModel = $bm->getVmBySlug($field->getOption('list'));
195  $list = $viewModel->getListModel();
196  $joins .= "
197  LEFT JOIN `".$field->getJoinTableName($list)."`
198  AS `".$list->getTableName().$field->getName()."`
199  ON (
200  `".$list->getTableName().$field->getName()."`.id = `".$this->getTableName()."`.".$field->getName()."
201  )";
202  }
203  }
204  return $joins;
205  }
206 
207  public function createFieldsToSelect($prefix = '') {
208  $bm = $this->getBm();
209  $fieldsToSelect = array();
210  foreach($this->getFields() as $field) {
211  if($field->getName()==null) {
212  continue;
213  }
214  if($field->getOption('calc') == true) {
215  continue;
216  }
217  $temp = "`".$this->getTableName()."`.`".$field->getName()."`";
218  if(!empty($prefix)) {
219  $temp .=" AS `".$prefix.$field->getName()."`";
220  }
221  if($field instanceof IdField) {
222  $viewModel = $bm->getVmBySlug($field->getOption('list'));
223  $list = $viewModel->getListModel();
224  $temp = $list->titleToSelect($field->getName(), $prefix);
225  $temp .= ",`".$list->getTableName().$field->getName()."`.`id` AS `".$prefix.$field->getName()."_id`";
226  }
227  $fieldsToSelect[] = $temp;
228  }
229  return $fieldsToSelect;
230  }
231 
232  public function titleToSelect($fieldName, $prefix = '') {
233  $single = $this->createSingle();
234  $titleField = $single->getTitleField();
235  return "`".$this->getTableName().$fieldName."`.`".$titleField->getName()."` AS `".$prefix.$fieldName."`";
236  }
237 
241  public function getBm() {
242  return $this->vmodel->getBm();
243  }
244 
245 // public function getTableName() {
246 // return $this->vmodel->getTableName();
247 // }
248 
249  public function getSlug() {
250  return $this->vmodel->getSlug();
251  }
252 
253 }
getTotal(IFilter $filter)
Definition: DataList.php:85
getTableName()
Definition: DataList.php:13
readOne($id, $column='id')
Definition: DataList.php:136
findOne(IFilter $filter)
Definition: DataList.php:72
getFieldNames()
Definition: DataList.php:55
createViewAs($sql)
Definition: DataList.php:181
createViewIfNeeded()
Definition: DataList.php:145
createFieldsToSelect($prefix= '')
Definition: DataList.php:207
fullfind(IFilter $filter)
Definition: DataList.php:113
createOrderBy($fields)
createTableIfNeeded()
Definition: DataList.php:163
find(IFilter $filter)
Definition: DataList.php:31
getOne($id, $column='id')
Definition: DataList.php:128
createWhere($fields)
__construct(IVModel $vmodel)
Definition: DataList.php:9
getMaxPages(IFilter $filter)
Definition: DataList.php:101
titleToSelect($fieldName, $prefix= '')
Definition: DataList.php:232