Note that there are some explanatory texts on larger screens.

plurals
  1. POZend Framework export Doctrine query results to XML file
    text
    copied!<p>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.</p> <pre><code>This page contains the following errors: error on line 3 at column 1: Extra content at the end of the document </code></pre> <p>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?</p> <p>My code is below</p> <p>My controller</p> <pre><code>public function init() { // allow certain reports to be exported to xml // initialize context switch helper $contextSwitch = $this-&gt;_helper-&gt;getHelper('contextSwitch'); $contextSwitch-&gt;addActionContext('newsletterexport', 'xml') -&gt;initContext(); </code></pre> <p>}</p> <pre><code>public function newsletterexportAction() { $q = Doctrine_Query::create() -&gt;select('c.firstname,c.lastname,c.address1,c.address2,c.address3,t.county') -&gt;from('PetManager_Model_Clients c') -&gt;leftJoin('c.PetManager_Model_Counties t') -&gt;where('c.consentToNews=1'); $result = $q-&gt;fetchArray(); if (count($result) &gt;= 1) { $this -&gt; view -&gt; records = $result; } } </code></pre> <p>EDIT</p> <p>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.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;petmanager:document xmlns:petmanager="http://petmanager"&gt;&lt;petmanager:records/&gt;&lt;/petmanager:document&gt; </code></pre> <p>My controller code as of this edit</p> <pre><code> public function newsletterexportAction() { $q = Doctrine_Query::create() -&gt;select('c.firstname,c.lastname,c.address1,c.address2,c.address3,t.county') -&gt;from('PetManager_Model_Clients c') -&gt;leftJoin('c.PetManager_Model_Counties t') -&gt;where('c.consentToNews=1'); $result = $q-&gt;fetchArray(); if (count($result) &gt;= 1) { //$this -&gt; view -&gt; 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-&gt;createElementNS('http://petmanager','petmanager:document'); $dom-&gt;appendChild($root); // convert to SimpleXML $xml = simplexml_import_dom($dom); // add resultset elements $records = $xml-&gt;addChild('records'); foreach($this-&gt;$result as $r){ $record = $records-&gt;addChild('record'); $record-&gt;addChild('firstName',$this-&gt;escape($r['firstName'])); $record-&gt;addChild('lastName',$this-&gt;escape($r['lastName'])); $record-&gt;addChild('address1',$this-&gt;escape($r['address1'])); $record-&gt;addChild('address2',$this-&gt;escape($r['address2'])); $record-&gt;addChild('address3',$this-&gt;escape($r['address3'])); $record-&gt;addChild('county',$this-&gt;escape($r['PetManager_Model_Counties']['county'])); }//end of foreach // saave document $xml-&gt;saveXML(); $dom-&gt;save('D:/reports/exports/'.$docname.''); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload