2011-08-02 32 views
0

这是第一次使用CakePHP的XML函数。CakePHP的Xml到数组到数据库

我已经建立了这种模式:

<?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数组类似:

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 
        ) 
        .... 

任何帮助,将不胜感激,即使你可以点我在正确的方向:)谢谢你们

戴夫

+0

所以有什么问题?您获得了数据,您可以对模型进行保存以将其保存在数据库中。 –

+0

但它怎么会知道在哪里把数据大声笑因为数据已被放到多个表和字段:) – Dave

+0

你告诉它:))好的,我会发布我的答案在下面。 –

回答

0

你不需要进口商模型

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循环来构建正确的数据格式以保存在你的模型中。我不知道你的表格模式,所以这是大方向。

+0

好吧,这是伟大的,我可以消除模型哈哈,所以我得到了那个和数组,但它通过它循环到它的格式我的表使用,我坚持。例如。交易表具有ID,标题,精彩集锦,精美打印,描述,价格,rrp_price <<<这些不同于数组lol:P谢谢 – Dave

+0

然后发布您的表架构。 –

+0

你只是想在我的数据库表和字段? – Dave