コード例 #1
0
ファイル: CSV.php プロジェクト: gitter-badger/tabulate-1
 /**
  * Assume all data is now valid, and only FK values remain to be translated.
  * 
  * @param DB\Table $table The table into which to import data.
  * @param array $column_map array of DB names to import names.
  * @return integer The number of rows imported.
  */
 public function import_data($table, $column_map)
 {
     global $wpdb;
     $change_tracker = new \WordPress\Tabulate\DB\ChangeTracker($wpdb);
     $change_tracker->open_changeset('CSV import.', true);
     $count = 0;
     $headers = $this->remap($column_map);
     for ($row_num = 1; $row_num <= $this->row_count(); $row_num++) {
         $row = array();
         foreach ($this->data[$row_num] as $col_num => $value) {
             if (!isset($headers[$col_num])) {
                 continue;
             }
             $db_column_name = $headers[$col_num];
             $column = $table->get_column($db_column_name);
             // Get actual foreign key value
             if ($column->is_foreign_key()) {
                 if (empty($value)) {
                     // Ignore empty-string FKs.
                     continue;
                 } else {
                     $fk_rows = $this->get_fk_rows($column->get_referenced_table(), $value);
                     $foreign_row = array_shift($fk_rows);
                     $value = $foreign_row->get_primary_key();
                 }
             }
             // All other values are used as they are
             $row[$db_column_name] = $value;
         }
         $pk_name = $table->get_pk_column()->get_name();
         $pk_value = isset($row[$pk_name]) ? $row[$pk_name] : null;
         $table->save_record($row, $pk_value);
         $count++;
     }
     $change_tracker->close_changeset();
     return $count;
 }