Example #1
0
 /**
  * Query ข้อมูลรายการเดียว
  * SELECT .... LIMIT 1
  *
  * @param array|string $fields (options) null หมายถึง SELECT ตามที่กำหนดโดย field
  * @return bool|array|Field ไม่พบคืนค่า false พบคืนค่า record ของข้อมูลรายการเดียว
  */
 public function first($fields = null)
 {
     $sqls = array('from' => $this->field->getTableWithAlias(), 'limit' => 1);
     if (!empty($fields)) {
         $qs = array();
         foreach (func_get_args() as $item) {
             if (!empty($item)) {
                 $qs[] = $this->fieldName($item);
             }
         }
         $sqls['select'] = empty($qs) ? '*' : implode(', ', $qs);
     } elseif (empty($this->sqls['select'])) {
         $sqls['select'] = '*';
     }
     $sqls = ArrayTool::replace($this->sqls, $sqls);
     $sql = $this->db()->makeQuery($sqls);
     $this->datas = $this->db()->customQuery($sql, true, $this->values);
     if (empty($this->datas)) {
         return false;
     } elseif ($this->toArray) {
         return $this->datas[0];
     } else {
         $class = get_class($this->field);
         return new $class($this->datas[0]);
     }
 }