Code snippets for symfony 1.x

Navigation

Doctrine_Hydrator_XmlDriver

Remember to register your hydrator first.

public function executeIndex(sfWebRequest $request)
  {
    $manager = Doctrine_Manager::getInstance();
    $manager->registerHydrator('xml', 'Doctrine_Hydrator_XmlDriver');
}
 

See http://www.doctrine-project.org/documentation/manual/1_2/en/data-hydrators

<?php
 
class Doctrine_Hydrator_XmlDriver extends Doctrine_Hydrator_ArrayDriver
{
    public function hydrateResultSet($stmt)
    {
        $array = parent::hydrateResultSet($stmt);
        return $this->arrayToXml($array);
    }
 
    /**
     * <pre>Converts an Doctrine array graph of the form:
     * .array
     * .  0 =>
     * .    array
     * .      'id' => '1'
     * .      'PersonaDomicilio' =>
     * .        array
     * .          0 =>
     * .            array
     * .              'id' => '1'
     * To a XML representation.
     * @param array $array
     * @return DOMDocument <pre> The XML with following structure:
     * . &lt;result&gt;
     * .   &lt;rootTable_Collection&gt;
     * .     &lt;rootTable&gt;
     * .       &lt;id&gt;&lt;/id&gt;
     * .       &lt;field1&gt;&lt;/field1&gt;
     * .       &lt;relatedTable_Collection&gt;
     * .         &lt;relatedTable&gt;
     * .         &lt;/relatedTable&gt;
     * .         &lt;relatedTable&gt;
     * .        &lt;/relatedTable&gt;
     * .         ::
     * .       &lt;/relatedTable_Collection&gt;
     * .     &lt;/rootTable&gt;
     * .     &lt;rootTable&gt;
     * .       ::
     * .     &lt;/rootTable&gt;
     * .     ::
     * .   &lt;/rootTable_Collection&gt;
     * . &lt;result&gt;
     * </pre>
     */
    public function arrayToXml(array $array) {
        $result = new DOMDocument();
        $rootNode = $result->createElement('result');
        $result->appendChild($rootNode);
        $iterator = new RecursiveIteratorIterator(
            new RecursiveArrayIterator($array),
            RecursiveIteratorIterator::SELF_FIRST
        );
        $prevLvl = 0;
        $component[$prevLvl] = $this->_queryComponents[$this->_rootAlias]['table']
            ->getComponentName();
        $obj = $result->createElement($component[$prevLvl] . '_Collection');
        $rootNode->appendChild($obj);
        foreach ($iterator as $k => $val) {
            $depth = $iterator->getDepth();
            if ($depth < $prevLvl) {
                for ($i = 0; $i < ($prevLvl - $depth); $i++) {
                    $obj = $obj->parentNode;
                }
            }
            if (! is_array($val)) {
                $son = $result->createElement($k, $val);
                $obj->appendChild($son);
            } else {
                if (is_numeric($k)) {
                    $son = $result->createElement($component[$depth]);
                } else {
                    $component[$depth + 1] = $k;
                    $son = $result->createElement($k . '_Collection');
                }
                $obj->appendChild($son);
                !empty($val) && $obj = $son;
            }
            $prevLvl = $depth;
        }
        return $result;
    }
}
 
by juan Manuel Fernandez on 2011-04-25, tagged doctrine  hydrator  orm