Exemple #1
0
 /**
  * Make a table.
  *
  * @param   string  $table
  * @param   array   $columns
  * @return  void
  */
 public static function make($table, $columns, $connection = null)
 {
     // check exists
     if (static::exists($table)) {
         // error
         trigger_error('Table already exists.');
         // return
         return false;
     }
     // Laravel provides a mechanism for building tables,
     // but only in the context of migrations which are run
     // at the command line.  The following is a makeshift
     // way of achieving the same thing using the same methods.
     // NOTE: I don't know how to make this work w/ a custom
     // connection.  Only working w/ default connection.
     $db = new \Laravel\Database\Schema\Table($table);
     $db->create();
     // for each column...
     foreach ($columns as $column) {
         // The makeup of the $columns array is to define
         // each field w/ the name as the key, and the
         // value containing both a type and a length.
         $type = $column['type'];
         $field = isset($column['field']) ? $column['field'] : null;
         $length = isset($column['length']) ? $column['length'] : null;
         // add to schema
         $db->{$type}($field, $length);
         // if index...
         if (isset($column['index'])) {
             if ($column['index'] == true) {
                 $db->index($field);
             }
         }
     }
     // execute
     \Schema::execute($db);
 }
Exemple #2
0
 /**
  * Convert CSV to database table.
  *
  * @param   string  $table
  * @param   boolean  $table_already_exists
  * @param   boolean  $clear_existing_records
  * @return  void
  */
 public function to_database($table = null, $table_already_exists = false, $clear_existing_records = false)
 {
     // if no pre-existing table defined...
     if (!$table_already_exists) {
         $t = new \Table($table);
         $t->create();
         foreach ($this->columns as $column) {
             // create column; default length is 200
             $t->string($column);
         }
         \Schema::execute($t);
     } else {
         // if clear existing records...
         \DB::query(sprintf("TRUNCATE TABLE `%s`", $table));
     }
     // foreach row...
     foreach ($this->rows as $value) {
         // add to table
         \DB::table($table)->insert($value);
     }
 }