我有一個叫做Category的模型,它有許多費用。我正在嘗試生成一個手風琴類型的界面,用戶可以在其中擴展類別並編輯費用。我可以生成輸入字段,但它們似乎不處於「編輯」模式,因爲它們沒有預先填充。無法獲取formhelper預先填充多條記錄
我在網上搜了一下,發現了一些相關的文章,比如這個CakePHP: How to update multiple records at the same time with the Form helper,還有這個CakePHP: Form helper with saveMany() to edit multiple rows at once。我試圖模仿他們的代碼,但沒有成功。
我CategoriesController指數函數看起來像這樣...
public function index($project_id) {
$data = $this->Category->find('all', array(
'conditions'=>array('Category.project_id' => $project_id),
'order'=>array('Category.category_name')
));
$this->set('categories', $data);
$this->request->data = $data;
}
我已閱讀,cakephp2需要$這個 - >請求 - >數據被設置表單助手工作。但在網上找到的例子中,每個人似乎都使用了模型名稱的複數形式,所以我也嘗試過。
我的分類\ index.ctp看起來像這樣。我還沒有進入「手風琴」階段。我只是試圖在屏幕上獲得預填充的輸入框。
$i=0;
foreach ($categories as $category)
{
echo $this->Form->input("Category.$i.category_id");
echo $this->Form->input("Category.$i.category_name");
$j=0;
foreach($category['Expense'] as $expense)
{
echo $this->Form->input('Expense.' . $j . '.expense_id');
echo $this->Form->input('Expense.' . $j . '.expense_name');
echo $this->Form->input('Expense.' . $j . '.dollar_amount');
echo $this->Form->input('Expense.' . $j . '.sqft_amount');
$j++;
}
$i++;
}
此代碼似乎迭代正確,因爲它吐出了正確的輸入字段。現在最大的問題就是讓字段預先填充。它似乎並沒有處於「編輯」模式,我擔心當我試圖保存數據時,這將成爲一個問題。
另外,我已經嘗試過,沒有$ this-> form-> create('Category')在頂部。這似乎沒有什麼區別。
的$類別陣列看起來像這樣...
array(
(int) 0 => array(
'Category' => array(
'category_id' => '1',
'category_name' => 'Category 1',
'category_index' => '1',
'project_id' => '1'
),
'Project' => array(
'project_id' => '1',
'project_name' => '131 Anndale Dr',
'project_sqft' => '1700',
'project_cost' => '318',
'project_cost_per_sqft' => '0'
),
'Expense' => array(
(int) 0 => array(
'expense_id' => '2',
'expense_name' => 'Nails',
'category_id' => '1',
'dollar_amount' => '50',
'sqft_amount' => '1',
'expense_index' => '2'
),
(int) 1 => array(
'expense_id' => '1',
'expense_name' => 'Wood',
'category_id' => '1',
'dollar_amount' => '99',
'sqft_amount' => '1',
'expense_index' => '1'
)
)
),
(int) 1 => array(
'Category' => array(
'category_id' => '3',
'category_name' => 'Category 2',
'category_index' => '2',
'project_id' => '1'
),
'Project' => array(
'project_id' => '1',
'project_name' => '131 Anndale Dr',
'project_sqft' => '1700',
'project_cost' => '318',
'project_cost_per_sqft' => '0'
),
'Expense' => array(
(int) 0 => array(
'expense_id' => '3',
'expense_name' => 'Bed',
'category_id' => '3',
'dollar_amount' => '99',
'sqft_amount' => '2',
'expense_index' => '1'
),
(int) 1 => array(
'expense_id' => '4',
'expense_name' => 'Chair',
'category_id' => '3',
'dollar_amount' => '70',
'sqft_amount' => '1',
'expense_index' => '2'
)
)
)
)
任何幫助將不勝感激。謝謝!!
非常感謝您的幫助!它工作完美。 – user2887380