DboSource::loadCsv PHP Method

loadCsv() public method

CSVファイルをDBに読み込む
public loadCsv ( array $options ) : boolean
$options array [ path / encoding ]
return boolean
    public function loadCsv($options)
    {
        $options = array_merge(array('path' => null, 'encoding' => $this->_dbEncToPhp($this->getEncoding())), $options);
        extract($options);
        if (!$path) {
            return false;
        }
        $table = basename($path, '.csv');
        $fullTableName = $this->name($this->config['prefix'] . $table);
        $schema = $this->readSchema(basename($path, '.csv'));
        if (isset($schema['tables'][$table]['indexes']['PRIMARY']['column'])) {
            $indexField = $schema['tables'][$table]['indexes']['PRIMARY']['column'];
        } else {
            $indexField = '';
        }
        $datas = $this->loadCsvToArray($path, $encoding);
        if ($datas) {
            foreach ($datas as $data) {
                $head = array();
                $values = array();
                foreach ($data as $key => $value) {
                    // 主キーでデータが空の場合はスキップ
                    if ($key == $indexField && !$value) {
                        continue;
                    }
                    if ($key == 'created' && !$value) {
                        $value = date('Y-m-d H:i:s');
                    }
                    if (!empty($schema['tables'][$table][$key]['type'])) {
                        $head[] = $this->name($key);
                        $values[] = $this->value($value, $schema['tables'][$table][$key]['type'], false);
                    }
                }
                $query = array('table' => $fullTableName, 'fields' => implode(', ', $head), 'values' => implode(', ', $values));
                $sql = $this->renderStatement('create', $query);
                try {
                    if (!$this->execute($sql)) {
                        return false;
                    }
                } catch (Exception $e) {
                    $this->log($e->getMessage());
                    return false;
                }
            }
        }
        return true;
    }
DboSource