0

使用 CakePHP,我需要检索一个基本的记录列表。没有连接或特殊的 CakePHP 结果格式,只有以下数据格式的基本记录数组:

[
    {first_name: 'Matthew', last_name: 'Stafford', gender: 'male'},
    {first_name: 'Jason', last_name: 'Hanson', gender: 'male'}
]

使用蛋糕模型最简单的方法是什么?

4

2 回答 2

1

通过适当的 MVC 分离,输出JSON 格式的结果如下所示:

控制器:

$people = $this->Person->find('all', array('conditions' => ...));
$this->set(compact('people'));

看法:

echo json_encode(array_map(function ($p) { return $p['Person']; }, $people));
于 2011-11-06T06:59:55.510 回答
0

我目前的解决方案是在 AppModel 中添加以下方法:

function selectAll($options = array()) {
    $this->recursive = 0;
    $result = $this->find('all', $options);

    return Set::combine($result, "{n}.{$this->name}.{$this->primaryKey}", "{n}.{$this->name}");
}
于 2011-11-07T15:10:15.453 回答