Cakephp Xml to Array to Database
this is the first time using CakePHP's XML functions.
I have set up this model:
<?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);
}
}
Controller:
<?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);
}
}
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] =&g开发者_如何转开发t; 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
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 }
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.
精彩评论