我正在构建一个 Cakephp 2+ web 应用程序。我已经使用 jquery 实现了包含来自用户的输入的动态添加/删除 div,现在想要将收集的数据保存在多个表中(现在使用 $belongsTo 的 2 个表)。问题是我无法按需要保存数据。它也不会显示在 debug 或 print_r 方法中。如果我在 jquery 或 cakephp 功能中有问题,请任何人帮助我。我到目前为止所做的如下:
jQuery
<script language='javascript'>
$(document).ready(function(){
var i=1;
var chk=0;
$('.btnAdd').click(function(event){
var inputHtml = '<div class="invoice2_bottom_inner" id="divin"><div class="invoice2_bottom_1"><div id="divno" style="margin:53px 0 0;">1</div></div><div class="invoice2_bottom_delete"><button id="cls" class="close btnRemove">×</button></div><div style="float:left; height:auto;width:660px;margin:0;"><table class="table invoice2_payee"><tr><td>Date</td><td>Payee</td><td></td></tr><tr><td><select class="span2" name="data[Expense][date][month][]" id="ExpenseDateMonth"><option value="01">January</option><option value="02">February</option><option value="03">March</option><option value="04">April</option><option value="05">May</option><option value="06">June</option><option value="07">July</option><option value="08">August</option><option value="09">September</option><option value="10">October</option><option value="11">November</option><option value="12" selected="selected">December</option></select>-<select class="span1" name="data[Expense][date][day][]" id="ExpenseDateDay"><option value="01">1</option><option value="02">2</option><option value="03">3</option><option value="04">4</option><option value="05" selected="selected">5</option><option value="06">6</option><option value="07">7</option><option value="08">8</option><option value="09">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option></select>-<select class="span2" name="data[Expense][date][year][]" id="ExpenseDateYear"><option value="2032">2032</option><option value="2031">2031</option><option value="2030">2030</option><option value="2029">2029</option><option value="2028">2028</option><option value="2027">2027</option><option value="2026">2026</option><option value="2025">2025</option><option value="2024">2024</option><option value="2023">2023</option><option value="2022">2022</option><option value="2021">2021</option><option value="2020">2020</option><option value="2019">2019</option><option value="2018">2018</option><option value="2017">2017</option><option value="2016">2016</option><option value="2015">2015</option><option value="2014">2014</option><option value="2013">2013</option><option value="2012" selected="selected">2012</option><option value="2011">2011</option><option value="2010">2010</option><option value="2009">2009</option><option value="2008">2008</option><option value="2007">2007</option><option value="2006">2006</option><option value="2005">2005</option><option value="2004">2004</option><option value="2003">2003</option><option value="2002">2002</option><option value="2001">2001</option><option value="2000">2000</option><option value="1999">1999</option><option value="1998">1998</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option></select></td><td><input class="input-medium" name="data[Expense][payee][]" maxlength="25" type="text" id="ExpensePayee"/></td><td></td></tr><tr><td>Type</td><td> Description</td><td>Amount</td></tr><tr><td><input class="span3" name="data[Expense][type][]" maxlength="25" type="text" id="ExpenseType"/></td><td><input class="input-medium" name="data[Expense][desc][]" maxlength="25" type="text" id="ExpenseDesc"/></td><td><span>$</span><input class="input-mini" name="data[Expense][amt][]" type="number" id="ExpenseAmt"/></td></tr></table></div></div>';
chk=1;
$('#divno').attr('id','divno'+i);
$('#divin').attr('id','divin'+i);
$('#cls').attr('id','cls'+i);
$('#divno'+i).html(i);
//alert(inputHtml);
event.preventDefault();
$('<div />',{'class':'invoice2_bottom_inner1', id:'div'+(i+1)}).append(inputHtml).appendTo('.invoice2_bottom');
//$('.invoice2_bottom').append(inputHtml);
$('.btnRemove').show();
//alert(i);
if(chk==1)
{
//alert(chk);
$('#divno').attr('id','divno'+(i+1));
$('#divno'+(i+1)).html(i+1);
$('#divin').attr('id','divin'+(i+1));
$('#cls').attr('id','cls'+(i+1));
chk=0;
}
i++;
var c=$('#cls'+i).attr('id');
$('#cls'+i).click(function(){
$('#divin'+i).remove();
//alert(c);
if(c=='cls2'){$('.btnRemove').hide();}
i--;
});
});
});
</script>
Cakephp
控制器:
debug($this->data);
$this->loadModel('Expense');
$this->Expense->create();
//if ($this->Expense->save($this->data)){
if ($this->Expense->saveAll($this->data['Expense'])) {
$this->Session->setFlash(__('The data has been saved**********'));
//$this->redirect(array('plugin'=>'agency_calendar','controller'=>'Events','action' => '/add/job/invoice/expense_reports'));
} else {
$this->Session->setFlash(__('The data could not be saved. Please, try again.'));
}
看法:
鉴于我正在使用包含用户输入的静态 div,在第一次加载页面时收集数据,并且在 jquery 的帮助下,我根据 Cakephp saveAll 方法将其制成数组,即 0、1、2 等等。
问候