I am trying to use importx to populate some new Template Variables.
I switched the system setting for importx to update (from create) BUT instead of just using the id to find records I get an error about needing the alias in the import csv.
CSV sample:
id; tv1; tv2
110; sometext; more text
Importx output:
Running pre-import tests on submitted data…
No errors in pre-import found. Preparing import values…
No errors found while checking the import values: 1 items found. Running import…
PHP notice: Undefined index: class_key
PHP notice: Undefined index: class_key
PHP notice: Undefined index: pagetitle
PHP notice: Undefined index: class_key
PHP notice: Undefined index: class_key
0 of 1 resources were imported successfully
Error: alias: This field is required.
Any help on overcoming this issue?
Thanks
Roy
I believe, ImportX uses the standard resource/update
-processor from MODx.
If you can’t provide the alias
, then it is probably easier to write your own snippet to import the TV-values.
Something like this might work:
<?php
$file_path = $modx->getOption('assets_path') . 'myimportfile.csv';
$output = array();
if (file_exists($file_path)) {
if (($handle = fopen($file_path, "r")) !== FALSE) { //open file
$row = 0;
$header;
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if ($row == 0){ //header row
$header = $data;
$row++;
continue;
}
$id = $data[0]; //assumes first column is 'id'
$resource = $modx->getObject('modResource', $id); //load resource
if ($resource) {
for ($idx = 1; $idx < count($header); $idx++) {
$tv_value = $data[$idx];
$tv_name = $header[$idx];
$resource->setTVValue($tv_name, $tv_value);
}
$row++;
} else {
$output[] = "Couldn't load resource with id " . $id;
}
}
fclose($handle);
$output[] = ($row - 1) . " rows imported";
$modx->cacheManager->refresh(); //clear cache
} else {
$output[] = "Couldn't open file " . $file_path;
}
} else {
$output[] = "Couldn't find file " . $file_path;
}
return implode('<br>', $output);
Sample for the file myimportfile.csv
. (Uses the name of the TVs instead of tv1
)
id;nameOfTv;anotherNameOfTv
110;sometext;more text