<?php
$modx->addPackage('goods', $modx->getOption('core_path').'components/goods/model/','cust_');
$url = 'assets/files/import/test.xml';
$xml = simplexml_load_file($url);
foreach ($xml->Good as $Good) {
$GoodGUID = $Good->GUID;
$GoodCode = $Good->Code;
$GoodCode = str_replace("," , '.', $GoodCode);
$GoodCode = preg_replace("/***91;^x\d|*\.***93;/", "", $GoodCode);
$GoodArticul = $Good->Articul;
$GoodTitle = trim($Good->Title);
$GoodPrice = $Good->Price;
$GoodPrice = str_replace("," , '.', $GoodPrice);
$GoodPrice = preg_replace("/***91;^x\d|*\.***93;/", "", $GoodPrice);
$GoodAmount = $Good->Amount;
$g = $modx->getObject('goods', array(
'guid' => $GoodGUID
));
if (!$g) {
print $GoodGUID.' - Item not found, added<hr />';
$g = $modx->newObject('goods', array(
'guid' => $GoodGUID,
'code' => $GoodCode,
'articul' => $GoodArticul,
'title' => $GoodTitle,
'price' => $GoodPrice,
'content' => '',
'alias' => '',
'amount' => $GoodAmount,
'published' => 1,
'hidemenu' => 0
));
} else {
print $GoodGUID.' - Item found, refreshed<hr />';
$g->fromArray(array(
'amount' => $GoodAmount
));
}
$g->save();
}
I try to import xml with this code, but have some problem, i think in this part:
$g = $modx->getObject('goods', array(
'guid' => $GoodGUID
));
This snippet adds only first item from xml to my empty mysql table, and all other xml items just refreshing this mysql row. I dont know why php (or xpdo?) thinks, that dfb3cc62-d709-11e1-86b8-4061865bb6fe is equal ccc8057a-d7b2-11e1-86b8-4061865bb6fe and every other string and how fix it.
dfb3cc62-d709-11e1-86b8-4061865bb6fe - Item not found, added
ccc8057a-d7b2-11e1-86b8-4061865bb6fe - Item found, refreshed
9a286cf8-daf0-11e1-86b8-4061865bb6fe - Item found, refreshed
f6378d08-dc85-11e1-86b8-4061865bb6fe - Item found, refreshed
06714e71-d7d5-11e2-885b-4061865bb6fe - Item found, refreshed
Thanks for helping
[ed. note: saeth.spb last edited this post 12 years, 4 months ago.]