开发者

Zend Framework export Doctrine query results to XML file

I have a need to export certain queries to xml files. I have this working in that the file is created and the data are exported, however I'm receiving the following error on screen as the file is being exported and not displayed.

This page contains the following errors:

error on line 3 at column 1: Extra content at the end of the document

I'll admit that I'm new to this as most of you are aware but is there a way I can export and just display a confirmation message to the user that the report has been saved, or am I going about this the wrong way completely?

My code is below

My controller

public function init()
{
  // allow certain reports to be exported to xml
  // initialize context switch helper
  $contextSwitch = $this->_helper->getHelper('contextSwitch');
  $contextSwitch->addActionContext('newsletterexport', 'xml')
              ->initContext();    

}

public function newsletterexportAction()

{
 $q = Doctrine_Query::create()
   ->select('c.firstname,c.lastname,c.address1,c.address2,c.address3,t.county')
  ->from('PetManager_Model_Clients c')
  ->leftJoin('c.PetManager_Model_Counties t')
  ->where('c.consentToNews=1');
  $result = $q->fetchArray();
         if (count($result) >= 1) {
    $this -> view -> records = $result; 
    }
 }

EDIT

Ok I tried moving the code from the xml.phtml into my controller as suggested and tried to save the document with save, but now I get the start of the xml document as shown below but no records are saved to the document.

<?xml version="1.0" encoding="utf-8"?>
<petmanager:document xmlns:petmanager="http://petmanager"><petmanager:records/></petmanager:document>

My controller code as of this edit

 public function newsletterexportAction()
 {
  $q = Doctrine_Query::create()

  ->select('c.firstname,c.lastname,c.address1,c.address2,c.address3,t.county')
  ->from('PetManager_Model_Clients c')
  ->leftJoin('c.PetManager_Model_Counties t')
  ->where('c.consentToNews=1');
    $result = $q->fetchArray();
         if (count($result) >= 1) {
          //$this -> view -> records = $result; 

          $docpref="newsletterexport";
           $docDate=date('y-m-d');
           $ext=".xml";
           $docname=$docpref.$docDate.$ext;

           // create XML document
           $dom = new DOMDocument('1.0', 'utf-8');

           // create root element
             $root = $dom->createElementNS('http://petmanager','petmanager:document');    
             $dom->appendChild($root);

              // convert to SimpleXML 
            $xml = simplexml_import_dom($dom);

            // add resultset elements
            $records = $xml->addChild('records');
            foreach($this->$result as $r){
          开发者_如何学Go  $record = $records->addChild('record');
            $record->addChild('firstName',$this->escape($r['firstName']));
            $record->addChild('lastName',$this->escape($r['lastName']));
            $record->addChild('address1',$this->escape($r['address1']));
            $record->addChild('address2',$this->escape($r['address2']));
            $record->addChild('address3',$this->escape($r['address3']));
            $record->addChild('county',$this->escape($r['PetManager_Model_Counties']['county']));
     }//end of foreach
      // saave document
      $xml->saveXML();
      $dom->save('D:/reports/exports/'.$docname.'');

    }


DomDocument::saveXML() doesnt take a file path - it returns the XML document as text. If you want to save it directly as a file like youre doing you use DomDocument::save().

All that logic in your phtml should be in your controller. You have also set the context to xml, so the view is going to output XML to the browser, not HTML... so id doesnt make sense to display a confirmation message unless its encoded in XML, otherwise you need to use the default context (HTML). So if you want to save the file to the webserver and display a confirmation you would do:

public function exportXmlAction(){
  // remove the context switch from init
  // do your doctrine stuff
  // build the xml DOM as $xml
  $xml->save('path/to/file');
  $this->message = 'File was exported successfully!';
}

// in your phtml
<?php echo $this->message; ?>

If you want to display the xml on screen:

public function exportXmlAction(){
  // do your doctrine stuff
  // build the xml DOM as $xml
  $this->xmlDom = $xml;
}

// in your phtml
<?php echo $this->xmlDom->saveXml(); ?>

The question i have though is whay would a user want ot export xml to the server. When you call DomDocument::save() youre saving that file to a path on the server, not the user's machine so what would be the point of exporting the xml to the server where the user has no access to it?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜