我有一个问题与我的编辑行动,我不知道我的问题是什么,但我的字段没有填充。在我的模型中,我有一个基于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')); ?>
当数据不为空时,您正在检索信息,但是当数据为空时(在第一次加载页面时),您只是填充id, reasons, contactType
。
public function edit($id = null) {
if (!empty($this->data)) {
//everything you posted
} else {
$this->request->data = $this->Declination->declinationsForPolicyId($id);
}
}
$reasons = $this->Declination->Reason->find('list');
$contactTypes = $this->Declination->ContactType->find('list');
$this->set(compact('id', 'reasons', 'contactTypes'));
}
我的问题解决了。我在整个申请过程中做了一些改变。我首先通过edit html链接传入策略id。之后,我的函数似乎起作用了,因为id被设置为策略id。此外,我对我的编辑功能进行了轻微的更改。您可以看到下面的更改。控制器
public function edit($id = null) {
if (empty($this->data)) {
$this->data = $this->Declination->declinationsForPolicyId($id);
} else {
// some code
$this->Session->setFlash(__('Declinations failed to save.', true));
}
}