也许这个问题已经存在,但到目前为止我还没能找到适合我的解决方案。
我想做的是以下内容。
创建数据集时,在" Save"旁边。按钮,我有一个值为选择框(5,10,15,20)。这应该确定使用相同内容创建相同记录的频率。除了应该清楚的ID。
这是否有效?如果是,我如何在控制器中实现它?
这是我的代码。
view.ctp
<?= $this->Form->button(__('Submit'),['class' => 'btn btn-primary']) ?>
<?php
echo $this->Form->select(
'copies', [
'1' => '1 time',
'5' => '5 times',
'10' => '10 times',
'15' => '15 times',
'20' => '20 times'
],
[
'label' => false,
'class' => 'form-control'
]
);
?>
<p class="help-block">How often should this record be created?</p>
字段&#34;副本&#34;只是我的变量。因此,数据库表中不存在复制字段。
感谢您的帮助
答案 0 :(得分:0)
//This is the data that comes from ur form
$data = $this->request->data;
//convert to int
$copies = (int)$data['copies'];
//Empty array that will be filled with data
$to_save = [];
//Time to loop!
for($i = 0; $i < $copies; $i++)
{
//Push the data into the array
$to_save[] = $some_data_array_to_be_inserted_that_probably_comes_from_the_request_data;
}
//Register the tableclass
$somethingTable = TableRegistry::get('Something');
//The saveMany() method of a tableClass works as a wrapper for save() method within a loop
if(!$somethingTable->saveMany($to_save))
{
//Error
echo 'Eita!';
}
else
{
//Success!
}