Cakephp Xml到数组到数据库 [英] Cakephp Xml to Array to Database

查看:158
本文介绍了Cakephp Xml到数组到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是第一次使用CakePHP的XML函数。

this is the first time using CakePHP's XML functions.

我设置了这个模型:

<?php
class Importer extends AppModel {
    var $name = 'Importer';
    var $useTable = false;

    function import_deals(){
        // import XML class
        App::import('Xml');

        // your XML file's location
        $file = "../webroot/files/datafeed_114532.xml";

        // now parse it
        $parsed_xml =& new XML($file);
        $parsed_xml = Set::reverse($parsed_xml); // this is what i call magic

        // see the returned array
        debug($parsed_xml);
    }
}

控制器:

<?php
class ImporterController extends AppController {

    var $name = 'Importer';
    var $uses = array('Importer', 'City', 'Deal', 'Partner');

    function import_deals($parsed_xml) {
        $this->loadModel('Importer');
        $deals = $this->Importer->find('all');

        $this->set('deals', $deals);
    }
}

视图我不确定,它把XML数据放入数据库。 XML数组类似于:

The view I am not sure about because I basically want it to put the XML data into the database. The XML array looks like:

Array (
    [MerchantProductFeed] => Array (
        [Merchant] => Array (
            [0] => Array (
                [id] => 2891
                [Prod] => Array (
                    [0] => Array (
                        [id] => 175029851
                        [web_offer] => yes
                        [Text] => Array (
                            [name] => London South: 60%   Off Takeaways From Just-Eat
                            [desc] => £6 for £15 Worth of Takeaways From Over 1000 London Eateries with Just-Eat
                            [promo] => 60 %
                        )
                        [Uri] => Array (
                            [awTrack] => http://www.awin1.com/pclick.php?p=175029851&a=114532&m=2891
                            [mImage] => http://static.groupon.co.uk/44/30/1311759403044.jpg
                        )
                        [Price] => Array (
                            [rrp] => 15.00
                        )
                        [Cat] => Array (
                            [awCatId] => 100
                            [awCat] => Bodycare & Fitness
                            [mCat] => Deals
                        )
                        [brand] => Array ()
                        [valFrom] => 2011-07-29
                        [valTo] => 2011-10-29
                    )
                    [1] => Array (
                        [id] => 175030161
                        [web_offer] => yes
                        [Text] => Array (
                            [name] => Essex: Up to 70% Off Treatment and Cut and Blowdry OR Half Head Highlights or Colour
                            [desc] => Cut, Blow Dry and Paul Mitchell Treatment (£18) Or Add Half Head of Highlights or Colour (£34) at Cube (Up to 70% Saving)
                            [promo] => 70 %
                        )
                        [Uri] => Array (
                            [awTrack] => http://www.awin1.com/pclick.php?p=175030161&a=114532&m=2891
                            [mImage] => http://static.groupon.co.uk/53/51/1311615285153.jpg
                        )
                        [Price] => Array (
                            [rrp] => 60.00
                        )
                        [Cat] => Array (
                            [awCatId] => 100
                            [awCat] => Bodycare & Fitness
                            [mCat] => Deals
                        )
                        [brand] => Array ()
                        [valFrom] => 2011-07-29
                        [valTo] => 2011-10-28
                    )
                    ....

任何帮助即使你可以指向我的方向:)谢谢你们

Any help would be appreciated, even if you can point me in right direction :) Thanks guys

Dave

推荐答案

您不需要Importer模型

You don't need Importer model


var $uses = array('City', 'Deal', 'Partner');
 function import_deals() {
   App::import('Xml');
   $file = "../webroot/files/datafeed_114532.xml";
   $xml_obj =& new XML($file);
   $parsed_xml = Set::reverse($xml_obj); 
   // set up the data here and save
}


基本上,如果xml数据的格式不同于数据库表(我猜是基于你的数据),你必须循环遍历$ parsed_xml来构造正确的数据格式保存在您的模型中。我不知道你的表模式,所以这是一般的方向。

Basically, if the xml data is not in the same format as the database tables (which I guess it is, based on your data), you'll have to loop through $parsed_xml to construct the correct data format to save in your model. I don't know your tables schema, so that's the general direction.

这篇关于Cakephp Xml到数组到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆