我的問題類似於this one。我明白那裏給出的答案。這個問題的OP似乎沒有我的問題。從CakePHP REST API中移除資源包裝器JSON
我正在使用CakePHP 2.2.3。我取這樣的資源:
http://cakephpsite/lead_posts.json
而且它返回結果是這樣的:
[
{
"LeadPost": {
"id": "1",
"fieldA": "blah",
"fieldB": "blah2",
}
{
"LeadPost": {
"id": "1",
"fieldA": "blah",
"fieldB": "blah2"
}
}
]
見每個對象的LeadPost
包裝?我不確定爲什麼它在那裏。我想刪除它。
LeadPost模型擴展AppModel,否則爲空。
LeadPostsController.php
class LeadPostsController extends AppController {
public $components = array('RequestHandler');
public function index() {
$records = $this->LeadPost->find('all', ['limit' => 20]);
$this->set(array(
'leadposts' => $records,
'_serialize' => 'leadposts'
));
}
}
我的路由很簡單:
Router::mapResources('lead_posts');
Router::parseExtensions();
你會不會碰巧知道爲什麼記錄首先被包裹起來?這似乎是非標準的,我找不到有關這個事實的任何CakePHP文檔。 –
@TylerCollier Cake的find方法總是(不包括'find('list')')別名索引來區分哪些模型數據屬於哪個。如果您使用可包含的行爲在相同的'find'中獲取關聯的模型,您將會看到每個表的數據都被別名正確索引。您可以做很多操作使用強大的Hash實用程序返回的結果來獲取更符合您需要的格式的數據。希望這能回答你的問題。不知道我解釋得很好。 – drmonkeyninja
來自文檔的實際操作示例:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasone – drmonkeyninja