0

我的players桌子上有三个field id, first_name, last_name。显示表中每个玩家的PlayersControllerhave 方法index

public function index() {
        $output = $this->Player->find('all');
        $this->set(array(
            'output' => $output,
            '_serialize' => array('output')
        ));
        $this->render('generic_response');
    }

generic_response 是一个 XML 视图,如下所示:

<?php
$xml = Xml::fromArray(array('response' => $output));
echo $xml->asXML();

生成的 XML 是:

<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>

但我想要类似的东西:

<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>

我怎样才能做到这一点?

4

1 回答 1

2

旧代码:

$output = $this->Player->find('all');
$this->set(array(
    'output' => $output,
    '_serialize' => array('output')
));

新代码:

$output = array(
    'Player' => Hash::combine(
        $this->Player->find('all'),
        '{n}.Player.id',
        '{n}.Player'
    )
);

$this->set(array(
    'players' => $output,
    '_serialize' => array('players')
));
于 2013-01-24T03:26:11.563 回答