/** * 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>'; }
/** * inint Class * * @param array $param */ public function __construct($param) { $this->id = 'datatable'; foreach ($param as $key => $value) { $this->{$key} = $value; } if (!empty($this->actions) && $this->checkCol == -1) { $this->checkCol = 1; } // รายการต่อหน้ามาจากการ POST หรือ GET if (isset($this->perPage)) { $this->perPage = \Input::get($_REQUEST, 'count', 30); } // header ของตาราง มาจาก model หรือมาจากข้อมูล หรือ มาจากการกำหนดเอง if (isset($this->model)) { // Recordset $this->rs = \Core\Orm\Recordset::create($this->model); // ฟิลด์ที่กำหนด (หากแตกต่างจาก Model) $this->rs->first($this->fields); // อ่านคอลัมน์ของตาราง $this->columns = $this->rs->getFileds(); } elseif (isset($this->datas)) { // อ่านคอลัมน์จากข้อมูลเราการแรก $this->columns = array(); if (!empty($this->datas)) { foreach (reset($this->datas) as $key => $value) { $this->columns[$key] = array('text' => $key); } } } // จัดการ header, ตรวจสอบกับค่ากำหนดมา เรียงลำดับ header ตาม columns if (!empty($this->columns)) { $headers = array(); foreach ($this->columns as $field => $attributes) { if (!in_array($field, $this->hideColumns)) { if (isset($this->headers[$field])) { $headers[$field] = $this->headers[$field]; if (!isset($headers[$field]['text'])) { $headers[$field]['text'] = $field; } } else { $headers[$field]['text'] = $field; } } } $this->headers = $headers; } }
/** * 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; }