我的目标是使用树对象来存储发票。我遇到的问题是它总是在每个后续子项周围放置一个“Invoice”模型包装器。我试着得到结果并使用Set类来清理冗余的“发票”,“孩子们”,但我无法弄清楚如何让它工作。我在API中使用它,所以基本上我想要做的就是在阵列中上下移动而不必经常发票.children.invoice.children
在我的控制器中我有:
$results = $this->Invoice->find('threaded', array(
'order' => array('lft ASC') // or array('id ASC')
));
$this->set(array(
'results' => $results,
'_serialize' => 'results'
));
我得到的是:
{
"success": true,
"data": [
{
"id": 1,
"parent_id": null,
"lft": 1,
"rght": 30,
"name": "Invoices",
"children": [
{
"Invoice": {
"id": 2,
"parent_id": 1,
"lft": 2,
"rght": 15,
"text": "invoice 001"
},
"children": [
{
"Invoice": {
"id": 3,
"parent_id": 2,
"lft": 3,
"rght": 8,
"text": "invoice_item"
},
"children": [
{
"Invoice": {
"id": 4,
"parent_id": 3,
"lft": 4,
"rght": 5,
"text": "invoice_sub_item"
},
"children": []
},
{
"Invoice": {
"id": 5,
"parent_id": 3,
"lft": 6,
"rght": 7,
"text": "subitem#2"
},
"children": []
}
]
}
]
}
]
}
]
}
理想情况下,我想
invoices.invoice.text = "invoice 001"
和
invoices.invoice.invoice_item[0].text = "invoice_item"
显然我需要添加/删除数组并重新提交更改。这是可能的还是我只是让自己变得更难?