Example #1
0
 public function create(array $fields, $delete = true)
 {
     $sql = '';
     if ($delete) {
         $delsql = 'DROP TABLE IF EXISTS ' . $this->table;
         $result = mysqli_query($this->db, $delsql);
         if (is_bool($result) && strlen($this->db->error)) {
             throw new Exception($this->db->error);
         }
     }
     $sql .= 'CREATE TABLE IF NOT EXISTS ' . $this->table . ' (
           `id` int(11) NOT NULL AUTO_INCREMENT,##fields##,
           `created_at` INT(11) unsigned DEFAULT NULL,
           `updated_at` INT(11) unsigned DEFAULT NULL,
           PRIMARY KEY (`id`)
         ) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
     $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 . '` varchar(255) DEFAULT NULL';
         } elseif ($type == 'int') {
             $sqlField = '`' . $name . '` int(11) unsigned DEFAULT \'0\'';
         } elseif ($type == 'double') {
             $sqlField = '`' . $name . '` double(11) unsigned DEFAULT \'0\'';
         } elseif ($type == 'float') {
             $sqlField = '`' . $name . '` float(11) unsigned DEFAULT \'0\'';
         } elseif ($type == 'text') {
             $sqlField = '`' . $name . '` text DEFAULT NULL';
         } elseif ($type == 'longtext') {
             $sqlField = '`' . $name . '` longtext DEFAULT NULL';
         } elseif ($type == 'date') {
             $sqlField = '`' . $name . '` date DEFAULT NULL';
         }
         $sqlFields[] = $sqlField;
     }
     $sql = str_replace('##fields##', implode(",", $sqlFields), $sql);
     $result = mysqli_query($this->db, $sql);
     if (is_bool($result) && strlen($this->db->error)) {
         throw new Exception($this->db->error);
     }
     return Db::instance($this->database, $this->table);
 }
Example #2
0
 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);
         }
     }
 }
Example #3
0
 public function pivot($table, $id)
 {
     return Db::instance($this->database, $table)->find($id, $this->model);
 }
Example #4
0
 public function __construct($ns)
 {
     $this->ns = $ns;
     $this->db = Db::instance('core', 'log');
 }