public static function __callStatic($method, $args) { $db = Inflector::uncamelize($method); if (fnmatch('*_*', $db)) { list($database, $table) = explode('_', $db, 2); } else { $database = SITE_NAME; $table = $db; } if (empty($args)) { return Db::instance($database, $table); } elseif (count($args) == 1) { $id = current($args); if (is_numeric($id)) { return Db::instance($database, $table)->find((int) $id); } } }
public function create(array $fields, $delete = true) { $sql = ''; if ($delete) { $delsql = 'DROP TABLE IF EXISTS ' . $this->table; $result = $this->db->exec($delsql); } $sql .= 'CREATE TABLE IF NOT EXISTS ' . $this->table . ' ( "id" integer(11) NOT NULL DEFAULT \'\',##fields##, "created_at" integer(11) NOT NULL DEFAULT \'\', "updated_at" integer(11) NOT NULL DEFAULT \'\' );'; $sqlFields = []; foreach ($fields as $name => $infos) { if (is_int($name)) { $name = $infos; $infos = []; } $type = isAke($infos, 'type', 'varchar'); if (fnmatch('*_id', $name)) { $type = 'int'; } if ($type == 'varchar') { $sqlField = '"' . $name . '" text NOT NULL DEFAULT \'\''; } elseif ($type == 'int') { $sqlField = '"' . $name . '" integer(11) NOT NULL DEFAULT \'\''; } elseif ($type == 'double') { $sqlField = '"' . $name . '" numeric NOT NULL DEFAULT \'\''; } elseif ($type == 'float') { $sqlField = '"' . $name . '" numeric NOT NULL DEFAULT \'\''; } elseif ($type == 'text') { $sqlField = '"' . $name . '" text NOT NULL DEFAULT \'\''; } elseif ($type == 'longtext') { $sqlField = '"' . $name . '" text NOT NULL DEFAULT \'\''; } elseif ($type == 'date') { $sqlField = '"' . $name . '" text NOT NULL DEFAULT \'\''; } $sqlFields[] = $sqlField; } $sql = str_replace('##fields##', implode(",", $sqlFields), $sql); $result = $this->db->exec($sql); return Db::instance($this->database, $this->table); }
public function __construct($ns) { $this->ns = $ns; $this->db = Db::instance('core', 'log'); }
public function related() { $fields = func_get_args(); foreach ($fields as $field) { if (fnmatch('*_*', $field)) { list($db, $table) = explode('_', $field, 2); } else { $table = $field; $db = SITE_NAME; } $fid = isAke($this->_data, $field . '_id', false); if ($fid) { $row = Db::instance($db, $table)->find((int) $fid, false); $this->_data[$key] = $row; } } return $this; }
public function pivot($table, $id) { return Db::instance($this->database, $table)->find($id, $this->model); }