CakePHP3 合并添加和编辑操作
CakePHP3 merge add and edit action
在我向系统添加 herd
后,用户被重定向到 herdimports
控制器,他可以在其中输入导入数据。
我想在一次操作和视图中添加和编辑表单。
在控制器中工作:
public function edit($herd_id = null)
{
if($herd_id == null)
{
// log error
$this->Flash->success(__('No herd was selected.'));
return $this->redirect(['action' => 'index']);
}
$herdimport = $this->Herdimports->find('all')->where(['herd_id'=>$herd_id]);
if($herdimport->count() == 0)
{
$herdimport = $this->Herdimports->newEntity();
}
if ($this->request->is(['patch', 'post', 'put'])) {
$herdimport = $this->Herdimports->patchEntities($herdimport, $this->request->getData());
$this->Herdimports->deleteAll(['herd_id'=>$herd_id]);
if ($this->Herdimports->saveMany($herdimport)) {
$this->Flash->success(__('The herdimport has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The herdimport could not be saved. Please, try again.'));
}
$this->set('herd_id', $herd_id);
$this->set(compact('herdimport'));
}
在视图中我有以下代码:
<?= $this->Form->create($herdimport) ?>
<fieldset>
<legend><?= __('Edit Herdimport') ?></legend>
<? $i = 0; ?>
<? foreach ($herdimport as $h) : ?>
<div class="repeat">
<?= $this->Form->hidden($i.'.herd_id'); ?>
<?= $this->Form->control($i.'.num',['data-default'=>""]); ?>
<?= $this->Form->control($i.'.date',['data-default'=>""]); ?>
<?= $this->Form->control($i.'.origin',['data-default'=>""]);?>
<?= $this->Form->control($i.'.weight',['data-default'=>""]); ?>
<?= $this->Form->control($i.'.price',['data-default'=>""]); ?>
</div>
<? $i ++; ?>
<? endforeach; ?>
<button class="extra-row"><?=__('Extra row');?></button>
<button class="delete-row" style="display: none;"><?=__('Delete row');?></button>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
当我有那个牛群的条目时,这非常有效。但是如果现在还有条目(添加案例),那么 foreach
将被忽略。
我如何检查视图是否有行。我试过 $herdimport->count()
,但是当没有行时会出错。
还试过$herdimport->isNew()
,当有行时会报错
有什么想法吗?
你可能不应该这样做:
$herdimport = $this->Herdimports->newEntity();
如果您想要 add/edit 多个项目,那么 $herdimport
应该始终是查询或实体列表,而不是单个实体。
如果基础 goal/problem 是要有一组初始输入以防还没有记录,那么您可以这样做:
$entity = $this->Herdimports->newEntity();
$entity->herd_id = $herd_id;
$herdimport = [$entity];
即简单地将初始实体包装在一个数组中(并确保填充外键字段)。
在我向系统添加 herd
后,用户被重定向到 herdimports
控制器,他可以在其中输入导入数据。
我想在一次操作和视图中添加和编辑表单。
在控制器中工作:
public function edit($herd_id = null)
{
if($herd_id == null)
{
// log error
$this->Flash->success(__('No herd was selected.'));
return $this->redirect(['action' => 'index']);
}
$herdimport = $this->Herdimports->find('all')->where(['herd_id'=>$herd_id]);
if($herdimport->count() == 0)
{
$herdimport = $this->Herdimports->newEntity();
}
if ($this->request->is(['patch', 'post', 'put'])) {
$herdimport = $this->Herdimports->patchEntities($herdimport, $this->request->getData());
$this->Herdimports->deleteAll(['herd_id'=>$herd_id]);
if ($this->Herdimports->saveMany($herdimport)) {
$this->Flash->success(__('The herdimport has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The herdimport could not be saved. Please, try again.'));
}
$this->set('herd_id', $herd_id);
$this->set(compact('herdimport'));
}
在视图中我有以下代码:
<?= $this->Form->create($herdimport) ?>
<fieldset>
<legend><?= __('Edit Herdimport') ?></legend>
<? $i = 0; ?>
<? foreach ($herdimport as $h) : ?>
<div class="repeat">
<?= $this->Form->hidden($i.'.herd_id'); ?>
<?= $this->Form->control($i.'.num',['data-default'=>""]); ?>
<?= $this->Form->control($i.'.date',['data-default'=>""]); ?>
<?= $this->Form->control($i.'.origin',['data-default'=>""]);?>
<?= $this->Form->control($i.'.weight',['data-default'=>""]); ?>
<?= $this->Form->control($i.'.price',['data-default'=>""]); ?>
</div>
<? $i ++; ?>
<? endforeach; ?>
<button class="extra-row"><?=__('Extra row');?></button>
<button class="delete-row" style="display: none;"><?=__('Delete row');?></button>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
当我有那个牛群的条目时,这非常有效。但是如果现在还有条目(添加案例),那么 foreach
将被忽略。
我如何检查视图是否有行。我试过 $herdimport->count()
,但是当没有行时会出错。
还试过$herdimport->isNew()
,当有行时会报错
有什么想法吗?
你可能不应该这样做:
$herdimport = $this->Herdimports->newEntity();
如果您想要 add/edit 多个项目,那么 $herdimport
应该始终是查询或实体列表,而不是单个实体。
如果基础 goal/problem 是要有一组初始输入以防还没有记录,那么您可以这样做:
$entity = $this->Herdimports->newEntity();
$entity->herd_id = $herd_id;
$herdimport = [$entity];
即简单地将初始实体包装在一个数组中(并确保填充外键字段)。