Reputation: 5056
I have the players
table with three field id
, first_name
, last_name
.
The PlayersController
have method index
that show every player in the table:
public function index() {
$output = $this->Player->find('all');
$this->set(array(
'output' => $output,
'_serialize' => array('output')
));
$this->render('generic_response');
}
and the generic_response is an XML view that look like this:
<?php
$xml = Xml::fromArray(array('response' => $output));
echo $xml->asXML();
The resulting XML is:
<response>
<output>
<Player>
<id>2</id>
<first_name>Ciro</first_name>
<second_name>Spee</second_name>
</Player>
</output>
<output>
<Player>
<id>3</id>
<first_name>Ugo</first_name>
<second_name>Ridi</second_name>
</Player>
</output>
</response>
but I want something like:
<response>
<players>
<Player>
<id>2</id>
<first_name>Ciro</first_name>
<second_name>Spee</second_name>
</Player>
<Player>
<id>3</id>
<first_name>Ugo</first_name>
<second_name>Ridi</second_name>
</Player>
</players>
</response>
How can I do this?
Upvotes: 0
Views: 1665
Reputation: 4839
Old code:
$output = $this->Player->find('all');
$this->set(array(
'output' => $output,
'_serialize' => array('output')
));
New code:
$output = array(
'Player' => Hash::combine(
$this->Player->find('all'),
'{n}.Player.id',
'{n}.Player'
)
);
$this->set(array(
'players' => $output,
'_serialize' => array('players')
));
Upvotes: 2