We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47819
    • 2 Posts
    <?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.]
      • 10487 MODX Staff
      • 1,535 Posts
      Replace:
      if (!$g) {

      With:
      if ($g == null) {
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 47819
        • 2 Posts
        replacing
        $GoodGUID = $Good->GUID;
        with
        $GoodGUID = (string) $Good->GUID;

        solved the problem.