我的编辑操作有问题,我不确定我的问题是什么,但我的字段没有填充。在我的模型中,我有一个基于 id 检索 3 条记录的函数,我在控制器中调用该函数,该函数又应在视图中填充数据。我试图调试但没有运气。我曾尝试使用 die(debug($this->data)); 但是我在运行脚本时会加载下一页而不是显示 $this->data 中的内容,这对我来说很奇怪!话虽如此,这就是我所拥有的。任何投入将不胜感激!谢谢!
模型
public function declinationsForPolicyId($id = null) {
if ($id) {
$items = $this->find('all', array(
'conditions' => array(
'Declination.policy_id' => $id
),
'limit' => 3,
'contain' => array()
));
foreach ($items as $item) {
$ret= $item['Declination'];
}
$ret = array('Declination' => $ret);
}
return $ret;
}
控制器
public function edit($id = null) {
if (!empty($this->data)) {
die(debug($this->data));
$this->Declination->create();
$this->data = $this->Declination->declinationsForPolicyId($id);
if ($this->Declination->saveAll($this->data['Declination'])) {
$this->Session->setFlash(__('Declinations saved.', true));
$this->redirect(array(
'controller' => 'policies',
'action' => 'view',
$id
));
} else {
$this->Session->setFlash(__('Declinations failed to save.', true));
}
}
$reasons = $this->Declination->Reason->find('list');
$contactTypes = $this->Declination->ContactType->find('list');
$this->set(compact('id', 'reasons', 'contactTypes'));
}
看法
<div class="content" id="container">
<?php echo $this->UiForm->create('Declination', array(
'url' => array(
'controller' => 'declinations',
'action' => 'add',
$id
)
)); ?>
<?php for ($i = 0; $i < 3; $i++): ?>
<section class="panel">
<h4>Declination <?php echo ($i + 1); ?></h4>
<?php echo $this->UiForm->input("Declination.{$i}.policy_id", array(
'type' => 'hidden',
'value' => $id
)); ?>
<?php echo $this->UiForm->input("Declination.{$i}.id", array(
'type' => 'hidden'
)); ?>
<?php echo $this->UiForm->input("Declination.{$i}.first_name"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.last_name"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.company"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.contact_type_id"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.phone_number"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.reason_id"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.other", array(
'label' => 'If other, please supply a reason'
)); ?>
<?php echo $this->UiForm->input("Declination.{$i}.dated", array(
'type' => 'text',
'readonly' => 'readonly',
'data-datepicker' => ''
)); ?>
</section>
<?php endfor; ?>
<?php echo $this->UiForm->end(array('label'=>'Continue', 'class'=>'btn success large fr')); ?>