--
http://www.matteolandi.net
On Nov 15, 2011 6:09 PM, "Will" <[email protected]> wrote:
> For the benefit of anyone else reading this, you can format the result
> of a call to $this->Model->find('all) with the following function:
>
> /** function _get_xml_array
> *
> * This function takes in an input from $this->Model->find() and
> reutrns an array
> * that is properly strucutred so that CakePHP's Xml Utility can
> format it.
> *
> * @author Will Simpson
> * @access private
> * @param $input -- An array from $this->Model->find()
> * @return $output -- An array in the following format:
> * array(
> * 'tags' => array(
> * 'tag' => array(
> * array(
> * 'id' => '1',
> * 'name' => 'defect'
> * ),
> * array(
> * 'id' => '2',
> * 'name' => 'enhancement'
> * )
> * )
> * )
> * );
> *
> */
> private function _get_xml_array($input,$plural=NULL,$singular=NULL)
> {
> $output = array();
>
> if(count($input) == 0)
> return $output;
>
> if($plural == NULL)
> $plural = $this->name;
>
> if($singular == NULL)
> $singular = key($input[0]);
>
>
> foreach($input as $i){
> $output[] = $i[$singular];
> }
> $output = array($plural=>array($singular=>$output));
> return $output;
> }
>
> Basically, the Xml Utility is expecting an array with one element that
> contains another array of one element that contains an array of many
> elements. Using my Employee model as an example, the correct
> structure would be:
>
> Array
> (
> [Employees] => Array
> (
> [Employee] => Array
> (
> [0] => Array
> (
> [id] => 1
> [emp_name] => Will Simpson
> [emp_job] => Web Developer
> )
>
> [1] => Array
> (
> [id] => 2
> [emp_name] => Joey Vickers
> [emp_job] => Random Guy
> )
>
> )
>
> )
>
> )
>
> If you use this structure, you don't need to be any "plural" tags in
> your xml view like to book describes. The view would simply be:
>
> <?php
>
> $xml = Xml::build($employees_xml);
> echo $xml->saveXML();
> ?>
>
>
> With no additional tags around it. Also make sure that you have the
> latest version of CakePHP (2.0.3). I think there is a hiccup in the
> RequestHandler in earlier versions that is causing the top-level xml
> tag to be repeated.
>
> --
> Our newest site for the community: CakePHP Video Tutorials
> http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help
> others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> [email protected] For more options, visit this group
> at http://groups.google.com/group/cake-php
>
>
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php