视图表单代码如下:
<tr id="research-items">
<td>
<select name="type[]">
<option value="text">单行文字</option>
<option value="textarea">多行文字</option>
<option value="radio">单项选择</option>
<option value="checkbox">多项选择</option>
</select>
</td>
<td><input type="text" name="question[]" value="" placeholder="这里是问题"></td>
<td><input type="text" name="items[]" value="" placeholder="答案1|答案2"></td>
<td>
<input type="checkbox" name="is_answer[]" value="1" <?php echo (isset($data) && $data['ResearchSetting']['is_answer']==1) ? "checked" : '' ; ?>>是否必填
</td>
<td><a class="btn_delete" title="删除" href="javascript:;">删除</a></td>
</tr>
控制器的写法如下:
for ($i=0; $i < count($_POST['type']) ; $i++) {
$post[$i]['ResearchOption']['type'] = $_POST['type'][$i];
$post[$i]['ResearchOption']['question'] = $_POST['question'][$i];
$post[$i]['ResearchOption']['items'] = $_POST['items'][$i];
$post[$i]['ResearchOption']['is_answer'] = ($_POST['is_answer'][$i])?$_POST['is_answer'][$i]:0;
$post[$i]['ResearchOption']['research_setting_id'] = $this->ResearchSetting->id;
}
$this->ResearchOption->saveAll($post);
参考链接:http://book.cakephp.org/2.0/en/models/saving-your-data.html
- 原文作者: forecho
- 原文链接: https://blog.forecho.com/cakephp-saveall-yong-fa.html
- 版权声明:本作品采用 署名-非商业性使用 4.0 国际 (CC BY-NC 4.0)进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。