/** * ORM Performance (select and update) * ทดสอบการเรียกข้อมูลและอัปเดทข้อมูลด้วย ORM */ private function orm() { $rs = \Core\Orm\Recordset::create('Index\\World\\Model'); $rs->updateAll(array('name' => '')); for ($i = 0; $i < 2; $i++) { $rnd = mt_rand(1, 10000); $result = $rs->find($rnd); $result->name = 'Hello World!'; $result->save(); } $result = $rs->find($result->id); echo $result->name; }
public function index() { // อ่านรายชื่อฟิลด์ของตาราง $model = Recordset::create('Index\\World\\Model'); $fields = $model->getFileds(); echo implode(', ', array_keys($fields)) . '<br>'; // ลบข้อมูลทั้งตาราง $model->truncate(); // insert new record for ($i = 0; $i < 10000; $i++) { $query = World::create(); $query->updated_at = \Datetool::mktimeToSqlDateTime(\Kotchasan::$mktime); $query->save(); } // อัปเดททุก record $model->updateAll(array('created_at' => \Datetool::mktimeToSqlDateTime(\Kotchasan::$mktime))); // อ่านจำนวนข้อมูลทั้งหมดในตาราง echo 'All ' . $model->count() . ' records.<br>'; // สุ่ม record มาแก้ไข for ($i = 0; $i < 5; $i++) { $rnd = rand(1, 10000); $world = $model->find($rnd); $world->name = 'Hello World!'; $world->save(); } // query รายการที่มีการแก้ไข $model->where(array('name', '!=', '')); // อ่านจำนวนข้อมูลที่พบ echo 'Found ' . $model->count() . ' records.<br>'; // แสดงผลรายการที่พบ foreach ($model->all('id', 'name') as $item) { echo $item->id . '=' . $item->name . '<br>'; // ลบรายการที่กำลังแสดงผล $item->delete(); } // อ่านรายชื่อฟิลด์ของ query $fields = $model->getFileds(); echo implode(', ', array_keys($fields)) . '<br>'; // อ่านจำนวนข้อมูลที่เหลือ echo 'Remain ' . Recordset::create('Index\\World\\Model')->count() . ' records.<br>'; }
/** * สร้างตาราง และเริ่มต้นทำงานตาราง * คืนค่าเป็นโค้ด HTML ของ Datatable * * @return string */ public function render() { $url_query = array(); $hidden_fields = array(); foreach ($_GET as $key => $value) { $value = rawurlencode($value); $url_query[$key] = $key . '=' . $value; // แอเรย์เก็บรายการ input ที่ไม่ต้องสร้าง if ($key !== 'search' && $key !== 'count' && $key !== 'page') { $hidden_fields[$key] = '<input type="hidden" name="' . $key . '" value="' . $value . '">'; } } if (isset($this->model)) { // รายการ Query หลัก (AND) $qs = array(); foreach ($this->defaultFilters as $array) { $qs[] = $array; } } // create HTML $content = array('<div class="datatable" id="' . $this->id . '">'); // form $form = array(); if (isset($this->perPage)) { $entries = \Language::get('entries'); $form[] = $this->addFilter(array('name' => 'count', 'text' => \Language::get('Show'), 'value' => $this->perPage, 'options' => array(10 => '10 ' . $entries, 20 => '20 ' . $entries, 30 => '30 ' . $entries, 40 => '40 ' . $entries, 50 => '50 ' . $entries, 100 => '100 ' . $entries))); } // รายการ Query กำหนดโดย User (AND) foreach ($this->filters as $key => $items) { $form[] = $this->addFilter($items); unset($hidden_fields[$items['name']]); // ไม่ Query รายการที่เป็นค่าว่าง if (isset($items['value']) && $items['value'] != '' && !empty($items['options'])) { if (in_array($items['value'], array_keys($items['options']))) { $qs[] = array($key, $items['value']); } } } // ปุ่ม Go if (!empty($form)) { $form[] = '<fieldset>'; $form[] = '<input type=submit class="button go" value="' . \Language::get('Go') . '">'; $form[] = implode('', $hidden_fields); $form[] = '</fieldset>'; } // search $search = \Input::text($_REQUEST, 'search'); if (!empty($this->searchColumns)) { if (!empty($search)) { if (isset($this->model)) { $sh = array(); foreach ($this->searchColumns as $key) { $sh[] = array($key, 'LIKE', "%{$search}%"); } $qs[] = $this->rs->group($sh, 'OR'); } elseif (isset($this->datas)) { // filter ข้อมูลจาก array $this->datas = \Arraytool::filter($this->datas, $search); } } $form[] = ' <fieldset class=search>'; $form[] = '<label accesskey=f class="icon-search"><input type=text name=search value="' . $search . '" placeholder="' . \Language::get('Search') . '"></label>'; $form[] = '<input type=submit value=" ">'; $form[] = '</fieldset>'; } if (!empty($form)) { $content[] = '<form class="table_nav" method="get">' . implode('', $form) . '</form>'; } if (isset($this->model)) { // Model $query = $this->rs->where($qs)->toArray(); if ($this->cache) { $this->rs->cacheOn(); } // จำนวนข้อมูลทั้งหมด $count = $this->rs->count(); } elseif (isset($this->datas)) { // จำนวนข้อมูลใน array $count = sizeof($this->datas); } else { $count = 0; } // การแบ่งหน้า if (empty($this->perPage)) { $start = 0; $totalpage = 1; $page = 1; $s = 1; $e = $count; $this->perPage = 0; } else { // หน้าที่เลือก $page = max(1, \Input::get($_REQUEST, 'page', 1)); // ตรวจสอบหน้าที่เลือกสูงสุด $totalpage = round($count / $this->perPage); $totalpage += $totalpage * $this->perPage < $count ? 1 : 0; $page = max(1, $page > $totalpage ? $totalpage : $page); $start = $this->perPage * ($page - 1); // คำนวณรายการที่แสดง $s = $start < 0 ? 0 : $start + 1; $e = min($count, $s + $this->perPage - 1); } // table caption if (empty($search)) { $caption = \Language::get('All :count entries, displayed :start to :end, page :page of :total pages'); } else { $caption = \Language::get('Search <strong>:search</strong> found :count entries, displayed :start to :end, page :page of :total pages'); } $caption = str_replace(array(':search', ':count', ':start', ':end', ':page', ':total'), array($search, number_format($count), number_format($s), number_format($e), number_format($page), number_format($totalpage)), $caption); // เรียงลำดับ $this->sort = \Input::get($_REQUEST, 'sort', $this->sort); if (!empty($this->sort)) { if (in_array($this->sort, array_keys($this->columns))) { $this->sortType = \Input::get($_REQUEST, 'sort_type', $this->sortType); $this->sortType = $this->sortType == 'desc' ? 'desc' : 'asc'; if (isset($this->model)) { $sort = isset($this->headers[$this->sort]['sort']) ? $this->headers[$this->sort]['sort'] : $this->sort; $query->order($sort . ' ' . $this->sortType); } elseif (isset($this->datas)) { \Arraytool::sort($this->datas, $this->sort, $this->sortType == 'asc'); } } } if (isset($this->model)) { // query ข้อมูล $this->datas = $this->rs->take($start, $this->perPage)->execute($this->fields); // รายการสุดท้าย $end = $this->perPage + 1; // รายการแรก $start = -1; } elseif (isset($this->datas)) { // รายการสุดท้าย $end = $start + $this->perPage - 1; // รายการแรก $start = $start - 2; } if (!empty($this->headers)) { // property ของ ตาราง $prop = array(); $c = array(); if (isset($this->class)) { $c[] = $this->class; } if ($this->border) { $c[] = 'border'; } if ($this->responsive) { $c[] = 'responsive-v'; } if ($this->fullWidth) { $c[] = 'fullwidth'; } if (sizeof($c) > 0) { $prop[] = ' class="' . implode(' ', $c) . '"'; } // table $content[] = '<div class="tablebody"><table' . implode('', $prop) . '>'; $content[] = '<caption>' . $caption . '</caption>'; $row = array(); $i = 0; $colCount = 0; $colspan = 0; foreach ($this->headers as $key => $attributes) { if ($colspan === 0) { if ($i == $this->checkCol) { $row[] = '<th class="check-column"><a class="checkall icon-uncheck"></a></th>'; $colCount++; } elseif ($i == $this->dragColumn) { $row[] = '<th></th>'; $colCount++; } if (isset($attributes['colspan'])) { $colspan = $attributes['colspan'] - 1; } $row[] = $this->th($i, $key, $attributes); $colCount++; $i++; } else { $colspan--; } } if (!empty($this->buttons)) { $row[] = $this->th($i, '', array('text' => '')); $colCount++; $i++; } if ($this->pmButton) { $row[] = $this->th($i, '', array('text' => '')); $colCount++; } // thead $content[] = '<thead><tr>' . implode('', $row) . '</tr></thead>'; // tbody $content[] = '<tbody>' . $this->tbody($start, $end) . '</tbody>'; // tfoot if ($this->checkCol > -1) { $row = array(); $row[] = '<td colspan="' . $this->checkCol . '"></td>'; $row[] = '<td class="check-column"><a class="checkall icon-uncheck"></a></td>'; $row[] = '<td colspan="' . ($colCount - $this->checkCol - 1) . '"></td>'; $content[] = '<tfoot><tr>' . implode('', $row) . '</tr></tfoot>'; } $content[] = '</table></div>'; $table_nav = array(); foreach ($this->actions as $item) { $table_nav[] = $this->addAction($item); } if (!empty($this->addNew)) { if (preg_match('/^((.*)\\s+)?(icon-[a-z0-9\\-_]+)(\\s+(.*))?$/', $this->addNew['class'], $match)) { $match[2] = trim($match[2] . ' ' . (isset($match[5]) ? $match[5] : '')); $table_nav[] = '<a class="' . $match[2] . '" href="' . $this->addNew['href'] . '"><span class="' . $match[3] . '">' . $this->addNew['text'] . '</span></a>'; } else { $table_nav[] = '<a class="' . $this->addNew['class'] . '" href="' . $this->addNew['href'] . '">' . $this->addNew['text'] . '</a>'; } } if (!empty($table_nav)) { $content[] = '<div class="table_nav action">' . implode('', $table_nav) . '</div>'; } // แบ่งหน้า if (!empty($this->perPage)) { $url_query['page'] = 'page=:page'; $url = '?' . implode('&', $url_query); $content[] = '<div class="splitpage">' . \Url::pagination($totalpage, $page, $url) . '</div>'; } } $content[] = '</div>'; // javascript ของ ตาราง $javascript = array(); $script = array('page' => $page, 'search' => $search, 'sort' => $this->sort, 'sort_type' => $this->sortType, 'action' => $this->action, 'actionCallback' => $this->actionCallback, 'actionConfirm' => $this->actionConfirm, 'pmButton' => $this->pmButton, 'dragColumn' => $this->dragColumn); $javascript[] = 'var table = new GTable("' . $this->id . '", ' . json_encode($script) . ');'; $content[] = "<script>\n" . implode("\n", $javascript) . "\n</script>"; return implode("\n", $content); }
/** * insert or update record */ public function save() { $class = get_called_class(); $recordset = Recordset::create($class); if ($this->exists) { $recordset->update(array($this->primaryKey, (int) $this->getAttribute($this->primaryKey)), $this); } else { $recordset->insert($this); } }
/** * INNER JOIN table ON .... * * @param string $model model class ของตารางที่ join * @param string $type เช่น LEFT, RIGHT, INNER... * @param mixed $on where condition สำหรับการ join * @return \Core\Orm\Recordset */ private function doJoin($model, $type, $on) { if (preg_match('/^([a-zA-Z0-9\\\\]+)(\\s+(as|AS))?[\\s]+([A-Z0-9]{1,2})?$/', $model, $match)) { $model = $match[1]; } $rs = Recordset::create($model); $table = $rs->tableWithAlias(isset($match[4]) ? $match[4] : null); $ret = $rs->buildJoin($table, $type, $on); if (is_array($ret)) { $this->sqls['join'][] = $ret[0]; $this->values = \Arraytool::replace($this->values, $ret[1]); } else { $this->sqls['join'][] = $ret; } return $this; }